Thread: Need help with reading from .txt

  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,273
    Try searching the board. This is a rather common question.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,364
    >> 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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Have you? Really?
    http://cboard.cprogramming.com/showp...63&postcount=5

    http://cboard.cprogramming.com/searc...earchid=597516 - 34 results, have you read everything and still found nothing? very strange...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

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, 07:27 AM
  3. reading strings from a binary .txt file
    By rwmarsh in forum C++ Programming
    Replies: 8
    Last Post: 03-05-2006, 09:23 PM
  4. Error after reading all words from a .txt
    By Axolotl in forum C Programming
    Replies: 9
    Last Post: 02-02-2003, 08: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