only first line being extracted from file

This is a discussion on only first line being extracted from file within the C++ Programming forums, part of the General Programming Boards category; hi there. i'm writing a small function that reads a file (how original ) i tried this but it's only ...

  1. #1
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332

    only first line being extracted from file

    hi there. i'm writing a small function that reads a file (how original ) i tried this but it's only returning the first line. and then quiting

    Code:
    string C_fsystem::readLine(void)
    {
    ifstream TMP_file;
    string TMP_buffer;
    TMP_file.open(fullName().c_str());
    if (TMP_file.fail()){cout<<"error opening file. quitting";cin.get();exit(1);}
    while (!TMP_file.eof())
          {
          getline(TMP_file,TMP_buffer);
          cout<<TMP_buffer;
          }
    TMP_file.close();
    }
    fullName() returns a string with the file name and everything goes fine. it read the first line then breaks off.

    any ideas?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, for one, your not returning the string. I'll just give a simple example and hold back on the explanations/critiqueing for now (or just let someone else do it). I don't use C++ streams as much as most folks, here (still pretty C, in that respect), so I'm sure you get even better, 'modern' examples from others, but here's an idea:

    Code:
     string read(const char * file)
    {
     string buffer, temp;
     
     ifstream in(file);
     
         if(in)
        {
             while(in >> temp)
            {
             buffer += temp + " ";
            } 
        }
     
     return buffer; 
    }
    
     int main(int, char ** argv)
    {
         while(*(++argv))
        { 
         cout << read(*argv) << endl;
        }
        
     return cin.get();
    }
    It's not really that efficient to return temporaries like that, though, and besides, you don't get an honest to god return value to tell you whether or not it failed, so I'd prefer the much more modest:


    Code:
    //returns the number of 'tokens' read
    
     int read(const char * file, string & buffer)
    {
     int result = -1;
    
     string temp;
     
     ifstream in(file);
    
     buffer = "";
     
         if(in)
        {
          result = 0; // start the counter
    
             while(in >> temp)
            {
             buffer += temp + " ";
     
             ++result;
            } 
        }
     
     return result; 
    }
    Hope that helps.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by whackaxe
    hi there. i'm writing a small function that reads a file (how original ) i tried this but it's only returning the first line. and then quiting

    Code:
    string C_fsystem::readLine(void)
    {
    ifstream TMP_file;
    string TMP_buffer;
    TMP_file.open(fullName().c_str());
    if (TMP_file.fail()){cout<<"error opening file. quitting";cin.get();exit(1);}
    while (!TMP_file.eof())
          {
          getline(TMP_file,TMP_buffer);
          cout<<TMP_buffer;
          }
    TMP_file.close();
    }
    fullName() returns a string with the file name and everything goes fine. it read the first line then breaks off.

    any ideas?
    If you wrote the file in binary mode and are reading it in text mode you could be having problems. If the data contains a text mode interpretation of a binary EOF character then it could be quiting prematurly thinking that it has reached the end of the file. Just a thought... Maybe you could post the file or the first few lines of it at least?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Try calling std::getline().

    while (std::getline(in, string)
    {}

    Kuphryn

  5. #5
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332
    kuphryn's solution seems to be working a charm. thank you very much

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by hk_mp5kpdw
    If you wrote the file in binary mode and are reading it in text mode you could be having problems. If the data contains a text mode interpretation of a binary EOF character then it could be quiting prematurly thinking that it has reached the end of the file.
    There is not an EOF character in files that says they are at the end of data. No matter what is being read or how it is being read, the only time the end of the file is reached is when the file pointer reaches the end of the data contained in the file.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by LuckY
    There is not an EOF character in files that says they are at the end of data. No matter what is being read or how it is being read, the only time the end of the file is reached is when the file pointer reaches the end of the data contained in the file.
    Then the following code when run on my machine is not doing what it is supposed to be doing:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        // Open the file for writing in binary mode
        ofstream output("Test.txt",ios::binary|ios::out);
        int data = 26;
        output << "This is before the int.";
        output.write((char*)&data,sizeof(int));    // Output int to the file, 26 = Ctrl-Z
        output << "This is after the int.";
        output.close();
    
        //  Open the file for reading now in text mode.
        ifstream input("Test.txt");
        string str;
        while( getline(input,str) )
            cout << str << endl;
        input.close();
    
        return 0;
    }
    Resulting output is:
    Code:
    This is before the int.
    If I remove the write of the variable data then the output becomes:
    Code:
    This is before the int.This is after the int.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No, Lucky's right. There isn't actually an EOF character. It's just a value that's returned by read functions to indicate that the file-pointer has reached the end of data. As to why the file seemed truncated when an int was reached was simply because the string now has a binary reprepresention of an integer between the two pieces of text and, since the integer contains three zero's before the '26' (remember there's four bytes to an int [usually]), 'cout' stops writing once it reaches one of those zero's (since strings are null-terminated). To see this yourself, just set the integer to 1094795585, and try the test again. You should see 'AAAA' between the text.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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