Templated function using ofstream prints nonsense.

Hello,

To generate output data, I'm printing a bunch of vector contents to files. Because the type of variable can differ between vectors, I wrote a templated printing function to print out whatever the content of the vector is. It looks like this:

1
2
3
4
5
6
7
8
9
10
template <class T>
void SomeClass::PrintVector(std::vector<T>& Values, std::string& outFile) {
	std::ofstream out(outFile, std::ios::app);
//	out.setf(std::ios_base::fixed);
	for (unsigned l = 0; l < Values.size(); ++l) {
		out << Values[l] << "\t";
		if (l % column_count == column_count-1) out << std::endl;
	}
	out.close();
}


I added the fixed because some larger values were being printed in scientific notation. Everything works well. My test code includes 3 vectors of doubles and 3 vectors of unsigneds. All the unsigneds work well and two of the doubles work well, but the third doubles vector prints nonsense unless I disable the fixed.

The calling code is the exact same. I know the values in the vector are correct, because a) if I comment out the "fixed" flag it works, and b) one of the unsigned vectors is sorted based on the values in that double vector (after it is printed, so the sort cannot corrupt the vector print) and works perfectly.

The "nonsense" looks like chinese/weird characters, if that matters.

Any ideas?
Can you show us a picture?

BTW, why on earth are you using l as a scrap variable name?
Setting both fixed and scientific at he same time generates output in hexadecimal floating-point format.

1
2
// out.setf(std::ios_base::fixed);
out.setf( std::ios_base::fixed, std::ios_base::floatfield ) ;


or simply: out << std::fixed ;
JLBorges wrote:
Setting both fixed and scientific at [the] same time generates output in hexadecimal floating-point format.
I am curious to know the explanation/reasoning for this?
You can explicitly set/get the bits of a float/double/etc this way, which is much more correct than using the base 10 conversions.
Uh, what? I thought it was just base-16 floating point, not the hex for the memory representation.
> I am curious to know the explanation/reasoning for this?

Using hexadecimal floating-point format avoids the tiny inexactness that may creep in during conversions: internal floating-point representation to sequence of decimal digits, and sequence of decimal digits back to internal floating-point representation.
@Duoas:

This is a screenshot of the output: http://imgur.com/SB9ybg1
[edit] The screenshot comes from Windows Notepad. I just discovered that it looks fine in Notepad++ and MS Word. I'm not sure if that's better or worse.

My biggest confusion is why it works in 2 out of 3 cases, but not in the third. The only difference is that the 3rd are shorter numbers (< 100 values, 2 decimals versus <1mil, 6 decimals).

(PS.: I'm using 'l' as an iterating variable because all my ranges have a meaning. 'l' is short for location, 't' means time, 'c' and 'r' are columns/rows, and so on. By keeping consistent letters for ranges I find nested loops to be much clearer. I guess the problem is that l looks like a 1 and I?)

Last edited on
Could it be Notepad's unicode detection bug?

http://en.wikipedia.org/wiki/Notepad_%28software%29#Unicode_detections
Has to be something like that.

Thanks for the interesting info, everyone. Marking this as solved!
Please, someone should help me with this assignment, thank you

You need to write a code, which will calculates results, depending on the values that user enters to the running software: i.e.
1) Y=1/A i.e Y – is result, A is a variable, and it is known, that the variable A has values from the interval: A_begin, A_end, and the step (A_step) which describes the step (speed) in which variables varies. Result should be printed out in „table“ that it has columns, and we can easy recognize, which column has values of Y, and which column has values of A, etc.

Let say; we have several examples: A_begining = 1, A_end = 4, A_step = 1, that the software should make calculations on several A values (Y=1/1, Y=1/2, Y=1/3, Y=1/4) and print out the values of A and result of Y, every time it will make calculations.

If we have A_begining = 1, A_end = -4, A_step = -1, your software should make all posible calculations with A values (1, 0, -1, -2, -3, -4) , and print result on spcreen. You need to make software that it will not HANG, and run correclty on any A values, that user will enter to it. All examples that you need to pay attention when caculations can be done, and when not, what condition of A_begin vs A_end and A_step should be analyzed where explain in 116 room.
The result in table could be provided such way (or like this)



!-----------------!---------------!

! value of A ! result Y !

!-----------------!---------------!

! 1 ! 1.0 !

! 2

And so on.

The same is for others three calculations.

Y=B/SQRT(A), B and A – provided bu user

Y = C/(B-A), all values provided bu user, BUT:

Y=xN X and N provided by user
You need to create your own thread instead of spamming someone else's.
Topic archived. No new replies allowed.