Thread: reading a float from file

  1. #1
    Registered User
    Join Date
    Feb 2003

    reading a float from file

    I'm having a problem reading float data type from file. The file is structured so that sets of float values would be separated by -99. (ie. 5.43, 6.78, 4.35, -99, 4.32, etc...). The -99 would flag the beginning of the next set. When I initially wrote the program, integers were saved to the file, and reading them back in were no problem. But then I modified the code to read and write floats, and it doesn't read them anymore. I've tried a couple of different ways:

       data >> temp; //data is file, temp is var
       data.getline (temp, sizeof(temp))
    but both methods skip all the floats and only read in the -99. temp is declared as float. Why is this happening? How can I read in the float values? I've checked around, but can't find a specific answer to this. Any help would be greatly appreciated.

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    maybe your temp variable is an worked fine for text file looked like this

    99 5.43 6.78 4.35
    and here is the code
    #include <iostream.h>
    #include <fstream.h>
    int main()
    	ifstream a_file("date.txt", ios::nocreate);
    	float var = 0.0f;
    	while(a_file.eof() == false)
    		a_file >> var; //should skip white spaces and store number;
    		cout << var << endl;
    	return 0;
    hope that helps
    nextus, the samurai warrior

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Replies: 8
    Last Post: 03-26-2007, 05:48 PM
  3. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  4. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  5. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM