ifstream in c++ ... more than one line in a single string

Sharky Forums


Results 1 to 2 of 2

Thread: ifstream in c++ ... more than one line in a single string

  1. #1
    Reef Shark wxjunkie's Avatar
    Join Date
    Nov 2000
    Location
    Lexington, Ky.
    Posts
    257

    Question ifstream in c++ ... more than one line in a single string

    How can I assign more than one line from a simple text file or more than one word on a single line in a text file to one declared string?

    Example... file.dat reads:
    Sharky Forums
    29 September 2002



    I want to assign "Sharky Forums" from that file to a string named refName

    ifstream inData;

    string refName;

    inData.open("file.dat")

    inData >> refName;


    How can I cause both words on the first line to be assigned to refName instead of just the first word, "Sharky" ??
    bash-2.03$ specs
    [windows]xp pro running on
    [ecs]K75SA
    [amd]athlon xp 1800+
    [nvidia]ati radeon 9200
    [crucial]512mb pc133
    [wd]80 gig, 120 gig
    [philips]15" 150 B2 Flat-Panel
    [yamaha]crw2400 16x10x40x
    [pioneer]slot 16x40x
    [soundblaster]live mp3+

  2. #2
    Catfish gonphsn's Avatar
    Join Date
    Sep 2002
    Posts
    159
    use the getline() function included in the string file.

    getline(infile (or whatever you ifstream is declared as), refName (or whatever youve declared your variable to be), '\n'(or whatever you want your halt character to be))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •