Thread: Read a txt file

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Read a txt file

    Hi I was never taught this, but is it possible to create a txt file on notepad for example and then use fstream to read it into a program? I do not want any code, just a link would be good to a tutorial. None of my books state this, they all go on about writng data too a file, which I already know how to do.

    Many thanks in advance. If it is not possible, please let me know also.
    Double Helix STL

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Check tutorials on file fstream, swgh. Particularly ifstream or Input/Output tutorials for a rundown on how to use both.

    Edit: err... don't you usually answer other posters on fstream anyway?
    Last edited by Mario F.; 12-15-2006 at 06:11 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Definitely possible, it is what I do for a "poor man's" graphical user interface sometimes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    As long as the file's been saved, then yes. It's the same as any other inreading(?).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> inreading(?)

    I think you meant outreading.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I'm not sure. I think he wanted to input the file.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Edit: err... don't you usually answer other posters on fstream anyway?
    True, but I had a programmers block really cant remember how to do it. Anyways thanks guys for the replys. I'l check the online tutorials for a refresher. I wonder why none of my books could help me on this?
    Double Helix STL

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    They probably do help you, you just never made the connection that all you are doing with an fstream is inputting and outputting text. Just pretend that whatever you typed into notepad was actually entered into the console and you were reading in with cin. It is the same idea.

    In fact, you say you're used to using fstream's with data, but if you outputted your data to a file with a .txt extension, you can open it up in Notepad and look at it as well.

    Unless you are doing binary output (and actually even then), whatever you write out from an ofstream can be read in Notepad, and whatever you type in Notepad can be read in with an ifstream.

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks daved. I must of overlooked what I was reading, I got it working now. Il have a good re-read over ifstream and make sure it all clicks. It never hurts to brush up.
    Double Helix STL

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I'd like to thank God, for making noodlebrains.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I'd like to thank God, for making noodlebrains.
    Il take that as tounge-in-cheek humor.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. read values from a txt file
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-30-2002, 01:37 PM