-
reading / writing files
I've just started playing with file io.
My raw data file contains the following:
-52.387596 61.659180 -2.277227
-52.341648 61.521450 -2.243313
.....
and I want to output a file such
#PSI Format 1.0
#
# column [0] = "x"
# column [1] = "y"
# column [2] = "z"
# column [3] = "Energy"
# column [4] = "Id"
# type[3] = byte
-52.3876 61.6592 -2.27723 0 0
As you can see from numbers in my outputed file the numbers have been rounded.
Q - How do I get around this?
Heres my code
Code:
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
int main (int argv, char* arvc[]){
char *sourceFileName = "start.xyz";
char *destinationFileName = "finish.psi";
fstream finput;
fstream foutput;
finput.open(sourceFileName, ios::in);
foutput.open(destinationFileName, ios::out);
int index = 0;
string line;
double
x, y, z;
if (finput.is_open() && foutput.is_open()){
foutput << "#PSI Format 1.0" << endl;
foutput << "#" << endl;
foutput << "# column [0] = \"x\"" << endl;
foutput << "# column [1] = \"y\"" << endl;
foutput << "# column [2] = \"z\"" << endl;
foutput << "# column [3] = \"Energy\"" << endl;
foutput << "# column [4] = \"Id\"" << endl;
foutput << "# type[3] = byte" << endl;
foutput << "" << endl;
while (!finput.eof()){
finput >> x >> y >> z;
foutput << x << " " << y << " " << z << " " << 0 << " " << index << endl ;
index++;
}
foutput.close();
finput.close();
}else{
cout << "Error in opening eithe input or output files";
if (finput.is_open ()){
finput.close();
}
if (foutput.is_open()){
foutput.close();
}
}
cout << "Conversion undertaken";
cin.ignore();
cin.ignore ();
return 1;
}
Perhaps I'm missing something real silly. I tried to make x,y,z floats as well but this didnt work either :-(
Thanks for pointers.
-
i believe you need to include the <iomanip> header and use setprecision(n). the default precision is 6 digits.
-
As I'm a real novice could you show some code as to how I would use this please?
I tried to use
Code:
finput.open(sourceFileName, ios::in);
finput.precision(8);
but I still have my number rounded. In fact cangin the precision doesnt seem to do anything to my inputed numbers.
Ta
-
Code:
#include <iomanip>
setprecision(n); // Where n is the number of digits it's rounded to
-
nope afriad that doesnt appear to do anything.
Any other pointers?
-
its been a bit but i think this should do it.
Code:
foutput.setf(ios::fixed);
foutput<<setprecision(n)<<double;
-
Code:
foutput << setprecision(8) << x << " " << y << " " << z << " " << 0 << " " << index << endl ;
-
Afraid that doesnt appear to do the job either.
-
Thanks for responses guys.
However the issue at the moment is getting the correct numbers read in.
As mentioned -52.387596 61.659180 -2.277227 is read in as
-52.3876 61.6592 -2.27723
-
mm not sure whats going on. Just tried swoopies code.
I use the following input
-52.387596 61.659180 -2.277227
-52.341648 61.521450 -2.243313
-52.383675 61.594273 -2.273051
-52.337238 61.465927 -2.241447
-52.379589 61.535492 -2.273933
-52.371269 61.574524 -2.207932
-52.373951 61.478222 -2.279162
in my outputted file I get
# PSI Format 1.0
#
# column [0] = "x"
# column [1] = "y"
# column [2] = "z"
# column [3] = "Energy"
# column [4] = "Id"
# type[3] = byte
-52.387596 61.65918 -2.2772269 0 1
-52.341648 61.52145 -2.2433131 0 2
-52.383675 61.594273 -2.273051 0 3
-52.337238 61.465927 -2.241447 0 4
-52.379589 61.535492 -2.2739329 0 5
-52.371269 61.574524 -2.207932 0 6
-52.373951 61.478222 -2.2791619 0 7
As you can see the numbers are not the same....
-
Here's my output file, making the one change above.
Code:
#PSI Format 1.0
#
# column [0] = "x"
# column [1] = "y"
# column [2] = "z"
# column [3] = "Energy"
# column [4] = "Id"
# type[3] = byte
-52.387596 61.65918 -2.277227 0 0
-52.341648 61.52145 -2.243313 0 1
-52.341648 61.52145 -2.243313 0 2
-
>So is there some rounding that goes on in the debugger? I have a watch on x, y, z and they are always rounded?
I never use a debugger, maybe someone knows why the Debugger rounds. :D
-
Well, you can do this if you want 6 digits after the decimal:
Code:
foutput << fixed << setprecision(6) << x << " " << y << " " << z << " " << 0 << " " << index << endl ;
-
I believe it will print properly, as long as your input contains six digits or less after the decimal. Same technique as Manofsteel showed.
-
Floating point numbers don't necessarily have an exact binary representaion so that is probably why you are losing some precision. If you need precision try a BinaryCoded Decimal library that will maintain the precision.
[edit]
Here is an MSDN article that explains.
http://support.microsoft.com/default...NoWebContent=1