Read a txt file

This is a discussion on Read a txt file within the C++ Programming forums, part of the General Programming Boards category; Hi I was never taught this, but is it possible to create a txt file on notepad for example and ...

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

    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.
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    22,271
    Definitely possible, it is what I do for a "poor man's" graphical user interface sometimes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  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,479
    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?
    I'm just trying to be a better person - My Name Is Earl

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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,479
    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.
    I'm just trying to be a better person - My Name Is Earl

  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.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    I'd like to thank God, for making noodlebrains.
    Il take that as tounge-in-cheek humor.
    I'm just trying to be a better person - My Name Is Earl

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, 04:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. read values from a txt file
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-30-2002, 02:37 PM

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