Need help with reading from .txt

This is a discussion on Need help with reading from .txt within the C++ Programming forums, part of the General Programming Boards category; I just started with c++ and now I need some help, I would like a simple script to read from ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    Unhappy Need help with reading from .txt

    I just started with c++ and now I need some help, I would like a simple script to read from a .txt and out the lines if my txt looks like this

    apple
    bannana
    peach
    ...
    ...

    put it in a loop and then out the lines
    Code:
    read("help.txt");
        while( doc is not at end )
        {
        string zin;
        zin = get the next line
        cout << zin
        }
    something like that.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Try searching the board. This is a rather common question.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> while( doc is not at end )
    This can cause problems when reading from a file, since there is no way of knowing if the doc is at the end until you have already tried to read past it.

    You want something like:
    Code:
    read("help.txt");
    string zin;
    while( (zin = get the next line) succeeds )
    {
        cout << zin
    }
    Lookup fstream and getline.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    meh

    I searched, but i couldnt find anything not so complicated. And I looked at fstream and getline, these seem fine, but I just cant put em to gether to get the desired effect.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Have you? Really?
    getline() help

    http://cboard.cprogramming.com/searc...earchid=597516 - 34 results, have you read everything and still found nothing? very strange...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading .txt file
    By vijay85 in forum C Programming
    Replies: 5
    Last Post: 03-09-2009, 04:07 PM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. reading strings from a binary .txt file
    By rwmarsh in forum C++ Programming
    Replies: 8
    Last Post: 03-05-2006, 08:23 PM
  4. Error after reading all words from a .txt
    By Axolotl in forum C Programming
    Replies: 9
    Last Post: 02-02-2003, 07:35 PM
  5. Reading maps from a .txt file?
    By Da-Spit in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2002, 04:23 AM

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