saving float array to dat file in a single operation

Hello Everyone,
I am trying to write a float array to a ‘*.dat’ file. I am only partially successful and would appreciate some help and explanation of what is going on wrong.
1) First setw command does not seem to work.
2) I can write float array to output file using one element at a time. But, I would like to write everything using a single operation.

First I initialize my float array
1
2
3
4
5
6
float * sliceProfile = new float[300];
then I calculate it 
  // Displaying calculated slice profile
	for(int i = 0; i != slabNz; ++i){
		std::cout << std::setw(7) << sliceProfile[i] <<std::endl;
	}


and it appears like this:
1
2
3
4
5
6
7
8
0.00302053
0.00333289
0.0035445
0.00363793
0.00360763
0.00345606
0.00319472
0.00284252


Here, there are more than 7 fields in output. So, I am not able to use setw as I wanted.

then try to save it using one of two possible ways:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  // This does not work
    std::ofstream outfile("OutputFile.dat");
	// write to outfile
	outfile.write ((char*)&sliceProfile, sizeof(float));
	outfile.close();

   // This works as desired
    std::ofstream outfile("OutputFile2.dat");
    // write to outfile
	for(int i = 0; i != slabNz; ++i){
		outfile << std::setw(4) << sliceProfile[i] <<std::endl;
	}

	outfile.close();


The file saved using the first method gives contains something like this:
 
0.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0


While the file saved using second method works, I do not want to read using for loop.

Could someone please explain:
1) Why setw is not working as expected:
2) how can I write all elements of a float array using a single operation and not inside the for loop.
Last edited on
Hi,

1) The std::setw function allows you to set the minimum width of the next output. This means that if your output has more than 7 digits, then nothing happens. If you want to display exactly 7 digits, then you should first of all truncate the number to the 7th digit. After this operation, std::setw should behave as you want.

2) Not sure about your question... Maybe what you want is serialization? But in any case, I think you have to traverse your array with a for loop.

By the way, this is an example on how to perform what I said about point (1):

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {

	double num = 0.00302053;

	int trunc_ = num * 10000000;
	double trunc = (double) trunc_ / 10000000;

	std::cout << std::setw(7);
	std::cout << num << std::endl;
	std::cout << trunc << std::endl;

	return 0;
}
Last edited on
outfile.write ((char*)&sliceProfile, sizeof(float));
should be
outfile.write ((char*)sliceProfile, 300*sizeof(float));
Note that it writes the data in binary format. Is that okay, or are you expecting to write it in ASCII?

Since you're writing a lot of data, don't use endl. It causes the data to be flushed which means a call to the operating system and possibly a write to the disk. Going out to the disk is about a million times slower than writing to RAM so the difference can be profound. Use '\n' instead:
outfile << std::setw(4) << sliceProfile[i] <<'\n';
Last edited on
@minomic , thanks a lot for explaining the meaning of setw.

@dhayden , thanks a lot for providing me solution.

Regards,
Dushyant

Topic archived. No new replies allowed.