Thread: reading / writing files

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    36

    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.

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    i believe you need to include the <iomanip> header and use setprecision(n). the default precision is 6 digits.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    36
    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
    Last edited by ozzy34; 09-29-2004 at 08:50 AM.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    Code:
    #include <iomanip>
    setprecision(n);       // Where n is the number of digits it's rounded to

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    36
    nope afriad that doesnt appear to do anything.

    Any other pointers?

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    its been a bit but i think this should do it.

    Code:
    foutput.setf(ios::fixed);
    foutput<<setprecision(n)<<double;
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    			foutput << setprecision(8) << x  << " " << y << " " << z << " " << 0 << " " << index << endl ;

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    36
    Afraid that doesnt appear to do the job either.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    36
    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

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    36
    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....
    Last edited by ozzy34; 09-29-2004 at 09:23 AM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well, you can do this if you want 6 digits after the decimal:
    Code:
    			foutput << fixed << setprecision(6) << x  << " " << y << " " << z << " " << 0 << " " << index << endl ;

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I believe it will print properly, as long as your input contains six digits or less after the decimal. Same technique as Manofsteel showed.

  15. #15
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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
    Last edited by manofsteel972; 09-29-2004 at 09:39 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading & Writing files (Error)
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2006, 11:55 AM
  2. Reading & Writing files
    By DaveHope in forum Linux Programming
    Replies: 8
    Last Post: 12-28-2003, 04:36 PM
  3. Having some trouble with reading from files
    By bluebob in forum C Programming
    Replies: 19
    Last Post: 04-18-2002, 05:29 AM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM