Dear friends

I have a conversion from ascii to binary while when i use my input which is like this :

16078 16283 0.3708
16078 16837 0.4514
16078 17820 0.4038
16078 17906 0.3695
16283 16837 0.3905
16283 17820 0.4343
16283 17906 0.3262
16837 17820 0.5291
16837 17906 0.6245
17820 17906 0.4709

but about 1 million lines, my output would grow incrementally in size. my input size is about 15 mg but the binary output would not stop and go to giga bytes. I dont know where is the error while my output must be exactly as it is in th original code. this 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();

	}

	if (argv[1][0]=='b') convertBack(argv[2],argv[3]);

	else if (argv[1][0]=='f') convertForth(argv[2],argv[3]);

}




int convertBack(char* binfile, char* asciifile){

	cout << "converting back" << binfile << "->" << asciifile <<endl<<flush;

	ifstream infile (binfile, ios::in|ios::binary);

	ofstream outfile (asciifile, ios::out);



	int index1;

	int index2;

	float simvalue;

	int counter=0;

	while (!infile.eof() ){

		counter++;

	  	if (counter%100000==0) {

	  		cout << counter << ".." << flush;

  		}

  		infile.read(reinterpret_cast<char *>(&index1),sizeof(int));

  		infile.read(reinterpret_cast<char *>(&index2),sizeof(int));

		infile.read(reinterpret_cast<char *>(&simvalue),sizeof(float));

		if (!infile.eof()) outfile << index1 << "\t" << index2 << "\t" << simvalue << endl << flush;

	}

	infile.close();

	outfile.close();

}



int convertForth(char* asciifile, char* binfile){

	cout << "converting forward" << asciifile << "->" << binfile <<endl<<flush;

	ifstream infile (asciifile, ios::in|ios::binary);

	ofstream outfile (binfile, ios::out|ios::binary);



	int index1;

	int index2;

	float simvalue;



	int counter=0;

	while (!infile.eof() ){

		counter++;

	  	if (counter%100000==0) {

	  		cout << counter << ".." << flush;

  		}

  		infile>>index1;

  		infile>>index2;

  		infile>>simvalue;

  		if (!infile.eof()){

  			outfile.write (reinterpret_cast<char *>(&index1), sizeof(int));

  			outfile.write (reinterpret_cast<char *>(&index2), sizeof(int));

  			outfile.write (reinterpret_cast<char *>(&simvalue), sizeof(float));

  		}

	}

	infile.close();

	outfile.close();

}

I would appreciate if you could let me know how shall I change my input file or code. Thanks in advance.

why are you opening the text files in binary mode??? Just open it in text mode

// not that ios::in and ios::out flags are not needed for ifstream and
// ofstream
ifstream infile (ascifile); 
ofstream outfile( binfile );
while( infile >> Index1 >> Index2 >> simvalue )
{
    outfile.write((char *)&Index1, sizeof(Index1);
    outfile.write((char *)&Index2, sizeof(Index2);
    outfile.write((char *)&simvalue, sizeof(simvalue);
}

Smart solution , yes right ... was really good idea :) well done and keep it up :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.