Your fscanf
is using the wrong format string (which GCC will tell you about if you enable sufficient warnings).
So your double
is filled with float
value, which of course leads to rather "random" errors.
If you change the "%g"
to "%lg"
, it should work just fine (at least it does on my Linux box).
Of course, if you use the C++ streams, e.g.
#include <fstream>
std::ofstream file1;
std::ifstream file2;
file1.open("output.txt");
for (i = 0; i < N;i++){
ref[i] = (double)i;
file1 << (double)i << std::endl;
}
and
file2.open("output.txt");
for(i = 0;i < N;i++)
file2 >> readIn[i];
the whole problem would have been avoided - and if you edit the readIn
variable to be float
, as long as the values are valid for that, it would be possible to read those values without chaning anything else [assuming the output is also using cout
instead of printf, of course].
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…