trouble opening a text file

This is a discussion on trouble opening a text file within the C++ Programming forums, part of the General Programming Boards category; Hi, I am having trouble trying to figure out how to open up a text file. I have read the ...

  1. #1
    Unregistered
    Guest

    trouble opening a text file

    Hi, I am having trouble trying to figure out how to open up a text file. I have read the tutorials but can't seem to get it to work properly. I am not doing anything with windows programming at all, strictly the console. Basically what I want to do is have a simple list of commands ex. (1) begin (2) shop (3)help and have it so that when you hit 3, it will open up the text file. Can anyone explain to me how this works or at least point me in the right direction, any help is appreciated. Thanks.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    sure.
    Code:
    #include <fstream>
    ...
    string line;
    
    ifstream help_file("help.txt",ios::in);
    while (getline(help_file,line))
      cout << line << endl;
    you'll probably have to tweak that somewhat

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    Try:

    #include<fstream.h>

    void main()
    {
    fstream HelpFile
    HelpFile.open("HelpFile.txt", ios :: out); //this opens the file named HelpFile for output
    }

    This will open the file for use as input; aka read from the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 12:45 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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