Dear experts
I have a large file with this format:
1 7252 0.1613
1 7253 0.1613
1 7254 0.0645
1 7255 0.0409
1 7256 0.0394
1 7257 0.1872
1 7258 0.1872
1 7259 0.0986
......
and I want to change this file to the binary format but it does not work for all file. it would only convert the first 100 kb of the file. here is my code.
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fstream>
#include <iostream>
using namespace std;
int convertBack(char* binfile, char* asciifile);
int convertForth(char* asciifile, char* binfile);
void printUsageAndExit(){
cout << "Usage" << endl;
cout << "conversion h\t\t\t\t\t(print this help)" << endl;
cout << "conversion b <binfile> <asciifile>\t\t(convert bin -> ascii)" << endl;
cout << "conversion f <asciifile> <binfile>\t\t(convert ascii -> bin)" << endl;
exit(0);
}
int main (int argc, char* argv[]){
if (argc<3||argv[1][0]=='h'){
printUsageAndExit();
}
else if (argv[1][0]=='f') convertForth(argv[2],argv[3]);
}
int convertForth(char* asciifile, char* binfile){
cout << "converting forward" << asciifile << "->" << binfile <<endl<<flush;
ifstream infile (asciifile);
ofstream outfile (binfile);
int index1;
int index2;
float simvalue;
while( infile >> index1 >> index2 >> simvalue )
{
outfile.write((char *)&index1, sizeof(index1));
outfile.write((char *)&index2, sizeof(index2));
outfile.write((char *)&simvalue, sizeof(simvalue));
}
infile.close();
outfile.close();
}
any suggestion is very very much appreciated. Best regards and thanks in advance.