read file? zeros

This is a discussion on read file? zeros within the C++ Programming forums, part of the General Programming Boards category; Why when reading from a file I get zeros after maybe reading half the data file? What could cause that? ...

  1. #1
    Question
    Guest

    Question read file? zeros

    Why when reading from a file I get zeros after maybe reading half the data file? What could cause that? Do I need to increase the buffer size or something?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Can you show us how you read the file?

  3. #3
    Question
    Guest

    Question

    Code:
     float input;
    
       /*open data file*/
    
         ifstream inFile("output");
       //inFile.seekg(0,ios::cur);
    
       inFile >> input;
       while(!inFile.eof())
      {
      inFile >> input;
       data.push_back(input); //vector
      inFile >> input;
     
        }
    Oh, I maybe on the wrong forum. But the code is listed here.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try taking out the second inFile>>input statement.
    Code:
    while(!inFile.eof())
    {
        inFile >> input;
        data.push_back(input); //vector
        //inFile >> input;
    }
    Also, how is the data file laid out?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    float input;
    
       /*open data file*/
    
         ifstream inFile("output");
       
     1)  inFile >> input;
       while(!inFile.eof())
      {
     2)  inFile >> input;
       data.push_back(input); //vector
     3)  inFile >> input
    removing this line makes more sense:

    2) inFile >> input;

    This technique allows you to check each individual input to see if it is EOF.

  6. #6
    Question
    Guest

    Question

    It's being laid out like

    Code:
    float p1;
    fprintf(data,"%f\n",p1);

    Thanks.

    One of my friend told me that when I open the file to read from it, whatever is in the file previously (before I execute the other program) is all that will be displayed, not the new data that is being written?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Unfortunately your last post isn't very clear in what you intended to say. With C++ you can't add material into the middle of a preexisting file without rewriting the entire file, but you can add to the end of a file. If you open the file, read data into the program from the file until you find the end of the file, then append more material to the end of the file, then read the stuff appended to the end of the file, it might work, though I haven't tried it.

    Code:
    //assume filename.txt exists and has data in it already
    ifstream fin("filename.txt");
    fin >> data;
    while(!fin.eof())
    {
      buffer.push_back(data);
      fin >> data;
    }
    //file all read into buffer at this point
    
    cout << buffer.size() << endl; //assume size is x.
    
    //open stream to append to the end of the file.
    ofstream fout("filename.txt", ios::app);
    
    //append second copy of data to end of file
    for(iterator = buffer.begin(); iterator != buffer.end(); ++iterator)
    {
      fout << *iterator;
    }
    
    fin.clear();//clear the EOF bit
    
    //start reading where you left off
    fin >> data;
    while(!fin.eof())
    {
      buffer.push_back(data);
      fin >> data;
    }
    
    cout << buffer.size() << endl; //size will probably be 2x now.
    
    //etc.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    > With C++ you can't add material into the middle of a
    > preexisting file without rewriting the entire file,

    Yes, you can. But you need a binary file (not text file) to do it, and it's a little more work.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    > With C++ you can't add material into the middle of a
    > preexisting file without rewriting the entire file,

    You can if you use binary files and seekg( ). You'd have to rewrite all of the data from the insertion point on, but you don't have to re-write the entire file.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. c script help to read a file & update
    By indy in forum C Programming
    Replies: 8
    Last Post: 12-01-2003, 10:32 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21