getline fuction

Discussion in 'Programming General' started by xbusterx, Nov 17, 2008.

getline fuction
  1. Unread #1 - Nov 17, 2008 at 10:49 PM
  2. xbusterx
    Referrals:
    0

    xbusterx Guest

    getline fuction

    >_<

    Code:
    
    int main()
    
    {   
       string name;
       cout << "Please enter the filename of the encoded message:";
       getline(cin,name);
    
       ifstream infile;
       infile.open(name.c_str());
    
       if (infile.fail()) {
          cout << name << " fail to open." << endl;
          exit(1);
       }
    
    cout << name << endl;
       cout << " File successfully opened" << endl;
    
       string buffer;
       getline(infile, buffer);
    
       while ( !infile.eof() ) {
       cout << buffer << endl;
       }
    
       return 0;
    }
    
    
    Hi I'm making a program where the user types in the name of a file and displays the context of the file in the console window. I'm trying to use getline function to read from the stream. The problem is that it reads the file and displays ONLY the first line ( it displays the first line, but it' an infinite loop) of the file to the console screen. How do I get it to display all the lines of the file to the terminal? I know I could use some type of loop, but I don't know what to do for the loop. So how do I display the other lines? Can someone plz change my code so that it displays all the lines? Thank You!!
     
  3. Unread #2 - Nov 17, 2008 at 11:28 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    getline fuction

    You need to include the getline() function inside the loop (either while or for doesn't matter) so it grabs the next line at every iteration. Here's the working code I ended up with after changing what you had for a minute or two.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    
    {   
       ifstream infile; 
       char * name;
       cout << "Please enter the filename of the encoded message: ";
       cin >> name;
    
    
       infile.open(name);
    
       if (infile.fail()) {
          cout << name << " failed to open." << endl;
          getchar();
          exit(1);
       }
       
       cout << name <<  " file successfully opened." << endl << endl;
       cout << "------------------------------------------------" << endl;
       char * buffer;
       for(int i = 1; !infile.eof(); i++) {
       infile.getline(buffer, 50);            
       cout << i << ": " << buffer << endl;
       }
       cout << "------------------------------------------------" << endl;
       getchar();
       getchar();
       return 0;
    }
    
    
     
  5. Unread #3 - Nov 22, 2008 at 12:39 PM
  6. Teh_Uber_Codex
    Joined:
    Nov 22, 2008
    Posts:
    75
    Referrals:
    0
    Sythe Gold:
    0

    Teh_Uber_Codex Member
    Banned

    getline fuction

    Woot fixed!

    ~Uber
     
< C++ 6.0 | Help with atan2f function! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site