Text 2 Binary

I need to do something that is really simple in matlab, yet i can't do it in c++.... newbie.
I am reading numbers (signed integers) from a txt ascii file and want to store them as short ints in a binary file (side by side). This is what I try:

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

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;

int main()
{	
	//Variable Declaration
	size_t fnd;
	int ncols, nrows, sz_filename;
	float xllcorner, yllcorner, cellsize, NaN;
	string curr_line, aux_string, in_filename, out_filename;
	
	cout << "\n";
	
	//Getting Filenames to handle
	cout << "Input filename: ";
	getline(cin,in_filename);
	cout <<"\n Output filename: ";
	getline(cin,out_filename);
	
      //Reading header file... pay no attention....
	ifstream infile (in_filename.c_str());//in_filename);
	if (infile.is_open())
	{
		for (int i=1;i<=6;i++)
		{
			//Scan each line of header (6 lines)
			getline (infile,curr_line);
			cout << curr_line << endl;	//show line
			
			//Extract values of interesting variables...
			fnd = curr_line.find("ncols");
			if( fnd!=string::npos  )
			{
				aux_string.assign(curr_line,14,32);
				stringstream(aux_string) >> ncols;
			}
			
			fnd = curr_line.find("nrows");
			if( fnd!=string::npos  )
			{
				aux_string.assign(curr_line,14,32);
				stringstream(aux_string) >> nrows;
			}

			fnd = curr_line.find("xllcorner");
			if( fnd!=string::npos  )
			{
				aux_string.assign(curr_line,14,32);
				stringstream(aux_string) >> xllcorner;
			}
			
			fnd = curr_line.find("yllcorner");
			if( fnd!=string::npos  )
			{
				aux_string.assign(curr_line,14,32);
				stringstream(aux_string) >> yllcorner;
			}
			
			fnd = curr_line.find("NODATA_value");
			if( fnd!=string::npos  )
			{
				aux_string.assign(curr_line,14,32);
				stringstream(aux_string) >> NaN;
			}
		}
		cout << "\n File header succesfully read!\n" <<endl;
		cout << "\n\n\n\n\n";
		cout << "The number of columns is " << ncols <<endl;
		cout << "The number of row is     " << nrows <<endl;
		cout << "Latitude of corner       " << xllcorner <<endl;
		cout << "Longitude of corner      " << yllcorner <<endl;
		cout << "Not-a-number number      " << NaN <<endl;

	}
	else
	{
		cout << "\n\nUnable to open file";
		return 0;
	}
	

    // ***********  THIS IS THE IMPORTANT PART OF THE CODE
	ofstream outfile (out_filename.c_str(), ios::out | ios::app | ios::binary);
	if (outfile.is_open())
	{
		short int curr_data;
		int iter=0;// progress;
  		double Niter;
		Niter=(double)(ncols*nrows)*100;
		double progress = 0;
		while (~infile.eof())
		{
			getline(infile,aux_string,' ');
			stringstream(aux_string) >> curr_data;
			if (curr_data == NaN)
			{
				curr_data = 0;  //Replace NaN with 0
			}
			
			outfile.write(reinterpret_cast<char *>(&curr_data),sizeof(short int));
			//outfile.write(&buffer,sizeof(short int));
			//outfile.write(&buffer,2);
			if (fmod((double)iter,Niter)==0)
			{
				progress++;
				cout << "\n" <<progress/Niter*100 <<"%";
			}
			iter++;
		}
	} //if offstream
	
	outfile.close();
	infile.close();
	return 0;
}


This creates a file that I can't read in the program I'm using to read these files and it's size is waaay to big (should be smaller than ascii file).

I'm using g++ to compile and running ubuntu linux (gutsy).

Any ideas? thank you
Last edited on
Open the file in a hex editor, and see if the numbers are in there at all.
Topic archived. No new replies allowed.