Thread: Reading a LINE from a file

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Reading a LINE from a file

    How can I make my program read a line from a file. I don't want it to stop on spaces, I want it to read the whole line.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    use getline()

    for character arrays:std::cin.getline(char*,size,delim)
    for Strings:getline(std::cin,string,delim)

    examples:
    Code:
    char linech[20];
    std::string linestr;
    
    std::cin.getline(linech,20,'\n');
    getline(std::cin,linestr,'\n');
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Wow, I think this will be the first time I found something useful from MSDN that actually deals with standard code

    http://msdn.microsoft.com/library/de...estlsample.asp

    and here is the code sample, this uses the C++ standard String class by the way.

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std ;
    
    int main()
    {
       string s1;
       cout << "Enter a sentence (use <space> as the delimiter): ";
       getline(cin,s1, ' ');
       cout << "You entered: " << s1 << endl;
    }

    basically, if you get rid of the third parameter (the ' ') then it will continue until it reaches a newline by default. such as the example below

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std ;
    
    int main()
    {
       string s1;
       cout << "Enter a sentence: ";
       getline(cin,s1);
       cout << "You entered: " << s1 << endl;
    }
    EDIT: Darn, beaten to it.
    Last edited by Xipher; 10-30-2005 at 01:10 AM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well I said "from a file", so...

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I thought this is problem is because I don't know how to use fgets, but:
    Code:
    while(fgets(run,100,pFile)){
    hello[ib]=run;
    ib=ib+1;
    }
    for(int i=0;i<7;i++){
    cout<<"begin\n"<<ib<<hello[i]<<"\nend"<<endl;
    }
    A part of my code. It doesn't read correctly, all pieces of hello are filled with the last line...

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    the getline example I used is usable with filestreams as well, just replace cin with the filestream

    EDIT
    Ok, I threw together a little demo of what I mean
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
            string InputBuffer;
            fstream InputFile;
            InputFile.open("testfile.txt", fstream::in);
            while(!InputFile.eof())
            {
                    getline(InputFile,InputBuffer);
                    cout << "Got one line: " << InputBuffer << endl;
            }
            InputFile.close();
            cout << endl << "All lines read and printed" << endl;
    }
    Yes, I know, the filename is hardcoded, but it worked to test with
    Last edited by Xipher; 10-30-2005 at 03:47 PM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by maxorator
    Well I said "from a file", so...
    string version of getline:
    getline takes a reference to an istream as its first parameter. It can be anything that can be reduced to an istream. ifstreams, iostreams, istringstream, etc all derive from istream which means they can all be used as the first parameter. Also on many systems your input and output are files.

    io version of getline:
    anything that inherits from istream also inherits the member function getline. So see above

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. reading file line by line
    By ameet in forum C Programming
    Replies: 7
    Last Post: 09-16-2004, 10:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM