Thread: Opening a text file

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Opening a text file

    Well for my final project I have to make a game and whatnot, that is kinda not the issue. The issue is how to get my .txt file open so people can read it. I've tried looking it up, but I must be doing something wrong, it keeps talking about file I/O (I guess as it should be), but I just want to be able to have my .txt file pop up when I click the button for the help file.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, you don't want your program to read the file, you want your user to read the file? I would guess this would have to be platform-specific; in Windows I think it's ShellExecute to open a text file using whatever the user's standard text-file-opener-program is.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Well I know in some games I've played they had it thatn when you click their button for help it would open a .txt file. I was hoping I could do the same, it doesn't sound like it though.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bwisdom View Post
    Well I know in some games I've played they had it thatn when you click their button for help it would open a .txt file. I was hoping I could do the same, it doesn't sound like it though.
    So, I take it you want your game to DISPLAY a particular text-file?

    If so, you need to read the file (using for example istream::getline()) and then display the text from that line. Keep going until the getline fails [at end of file].

    Should be possible to do in about 4-6 lines of code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Like matsp sufggested, somthing like the below would be similar to a loop you could develop.

    Code:
    while ( std::getline( inFile, line )) 
    {
       std::cout<< line << std::endl;
    }
    inFile being the name of the file to read in. But remember to open and close the file
    Double Helix STL

  6. #6
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    For windows, try system("type -FILENAME-");
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Oh awesome, so there is hope. The only reason I don't want to do it the windows way is because even though I am creating on a windows machine the project is going to be compiled and ran for a grade on a unix machine.

    Thank you all for the help. Ill try out this method and see how it goes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM