Reading these 2 lines...

This is a discussion on Reading these 2 lines... within the C++ Programming forums, part of the General Programming Boards category; Hi, all: I have the following 2 lines in a file: password Another sample line. I want to first read ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    9

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

    Hi, all:

    I have the following 2 lines in a file:

    password
    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?

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

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

    ...

    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
    Posts
    9,796
    >Can someone provide an example please?
    Code:
    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
    Posts
    2,787
    i think they mean they want it in seperate variables... if you want that:

    Code:
    ...
    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
    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

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-20-2008, 11:58 PM
  3. A problem with reading multiple lines from a file
    By edd1986 in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2006, 12: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

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