how do you know it does not convert the entire file? Did you write another program to read back all that binary data and compare it with the original file?
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
open file with ios::binary mode.
ofstream outfile;
outfile.open(binfile,ios::out | ios::binary);
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
Please write suitable code to solve your problem and if you got problem then post your problem.
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
but any solution so my problem and my codes? any C monk here? ;)
Had to open your file in binary mode?
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
Never say thanks :D This code is written by you.
#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;
outfile.open(binfile,ios::out | ios::binary);
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();
}
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
The bin file might be smaller than the text file because each integer written in binary mode only requires 4 bytes of file space (sizeof(int)) or 6 bytes for a float sizeof(float), while the same integer in a text file will require one byte per digit plus spaces between numbers and line terminating characters '\n'. The only way to predict the size of the binary file is to count up the size of all the integers, floats and doubles.
It might be interesting if you would zip of the text file and post it so that we had something more concrete to work with.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I wrote this program to generate the text file
int main()
{
srand( (unsigned int)time(0));
double x = 0;
ofstream out("data.txt");
for(int i = 0; i < 8000000; i++)
{
x = (double)rand() / (double)RAND_MAX;
out << i+1 << " " << i+7000 << " " << x << "\n";
if( i > 0 && (i % 100000) == 0)
cout << i << "\n";
}
}
And this one to convert it to bin file
int convertForth(char* asciifile, char* binfile);
int main (int argc, char* argv[])
{
convertForth("data.txt", "data.bin");
}
int convertForth(char* asciifile, char* binfile){
cout << "converting forward" << asciifile << "->" << binfile <<endl<<flush;
ifstream infile (asciifile);
ofstream outfile (binfile, ios::binary);
time_t t1, t2;
int index1;
int index2;
int i = 0;
float simvalue;
t1 = time(0);
while( infile >> index1 >> index2 >> simvalue )
{
outfile.write((char *)&index1, sizeof(index1));
outfile.write((char *)&index2, sizeof(index2));
outfile.write((char *)&simvalue, sizeof(simvalue));
if( i > 0 && (i % 100000) == 0)
cout << i << "\n";
i++;
}
infile.close();
outfile.close();
t2 = time(0);
cout << "Execution time: " << t2 - t1 << " seconds\n";
return 0;
}
The original text file was 200 meg and the bin file 94K. The second program took 112 seconds to convert from text to binary.
With an 800meg text file you might want to check your hard drive to see if it contains sufficient space to write out the data. Your description of the problem sounds like your hard drive might be full.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I have created dummy text file like OP source (~572 Mb, 40000000 lines) then converted it to binary form with VC++ 2008 for ~3.5 minutes without special buffer control. That's OK, no problems.
1. Open both files in binary mode.
2. Inspect source file with hex editor. Probably it's corrupted. Test your disk drive integrity.
What compiler are you using?
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
>>However, have you changed your binary file back to the txt file?
Yes, and both files were exactly the same (other than one being text and the other binary).
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
template <class T>
bool getnum(ifstream& in, T& num)
{
in.read((char*)&num, sizeof(T));
if( in.gcount() != sizeof(T))
{
cout << "Read error\n";
return false;
}
return true;
}
int main()
{
ifstream in1("data.txt");
ifstream in2("data.bin", ios::binary);
int index1;
int index2;
int i = 0;
float simvalue;
while( in1 >> index1 >> index2 >> simvalue )
{
int a, b;
float c;
if( !getnum( in2, a) || !getnum( in2, b)
|| !getnum( in2, c) )
{
cout << "read failed on line " << i << "\n";
break;
}
++i;
if( a != index1 || b != index2 || c != simvalue)
{
cout << "Values are not the same\n";
cout << a << " " << index1 << "\n";
cout << b << " " << index2 << "\n";
cout << c << " " << simvalue << "\n";
break;
}
if( (i % 16000) == 0)
cout << i << "\n";
}
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343