read from text file and change to binary

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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#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.
Topic archived. No new replies allowed.