Thread: reading from file

  1. #1
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    reading from file

    How do i read from a text file?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    char Buffer[256];
    ifstream File;
    File.open("MyFile.txt", ios::in);
    
    if(!File.fail())
    {
       while(!File.eof())
       {
          File >> Buffer;
          cout << Buffer << " ";
       }
       File.close();
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    thanks

    the ios::in was the bit i was after thanks

  4. #4
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    Buffer overflow

    A buffer overflow instatnly crashes it, any way around it?

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Replace File >> Buffer; with
    File.getline(Buffer, 255, EOF);

  6. #6
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    meep

    can u xplain what that does plz it seems rather weird

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Whatch out when using Microsoft VC++ 6.0

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: meep

    Originally posted by krappykoder
    can u xplain what that does plz it seems rather weird
    getline() reads a line from the file (with a max of 255 chars) of course .

  9. #9
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    meep

    can i read from a non-static line in a file so it searchs for
    password=

    or whatever?

  10. #10
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    Whatch out when using Microsoft VC++ 6.0
    What you have to be careful about is actually using >> to read/inpout since it reads only to the character return not skipping it while getline() reads the entire line and skips past the character return.

  11. #11
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by correlcj
    What you have to be careful about is actually using >> to read/inpout since it reads only to the character return not skipping it while getline() reads the entire line and skips past the character return.
    But that's what it's suppose to do (read the manual pages). I'm just saying the geline function in VC++ 6.0 is not working like descibed in the manual pages because it has a bug!

  12. #12
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Relax Moster!

    But that's what it's suppose to do (read the manual pages). I'm just saying the getline function in VC++ 6.0 is not working like descibed in the manual pages because it has a bug!
    And I am agreeing with you, mi amigo! No disputes here!

  13. #13
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    meep

    my question any1?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM