Thread: Reading these 2 lines...

  1. #1
    Registered User
    Join Date
    Apr 2003

    Reading with getline()..STILL NEED HELP =(

    Hi, all:

    I have the following 2 lines in a file:

    Another sample line.

    I want to first read in the first line (i.e. password) and write it to a file. Then I want to go back and read the second line and write only that second line to a file.

    How can I do this?

    How can each line be interpreted separately?

    Last edited by cpluspluser; 05-26-2003 at 09:29 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Ontario, Canada
    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

  3. #3
    Registered User
    Join Date
    Apr 2003


    I know I'm using getline(), but I can't seem to use it for 2 lines. I can get the first line no prob

    e.g. infile.getline(variable, 80, '\n')

    But how do I use getline() to get the second line and play with the 2nd line independently of the first?

    Can someone provide an example please?

    Much appreciated.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >Can someone provide an example please?
    while (getline(var, sizeof (var), '\n'))
        cout<< var <<endl;
    Every call to getline moves the 'get' pointer for the stream to the next available character after the delimiter. If it didn't then you would simply read the same line over and over, and that would just be silly.

    On a side note, don't double post. If we see your question more than once we may get confused and forget we saw it at all. I'm notorious for such lapses in memory.
    My best code is written with the delete key.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    i think they mean they want it in seperate variables... if you want that:

    char password[...];
    char line[...];
    cin.getline(password, ... ,'\n');
    cin.getline(line, ... ,'\n');
    then just use password and line as two different variables...
    Join is in our Unofficial Cprog IRC channel
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  3. A problem with reading multiple lines from a file
    By edd1986 in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2006, 01:26 PM
  4. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  5. Reading specific lines
    By Aristotle in forum C Programming
    Replies: 2
    Last Post: 04-12-2004, 04:34 PM