Reading User Defined Files

This is a discussion on Reading User Defined Files within the C++ Programming forums, part of the General Programming Boards category; Hi all. I was writing a simple program at school, and I got stuck on reading text files. I know ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Reading User Defined Files

    Hi all.

    I was writing a simple program at school, and I got stuck on reading text files. I know ... pathetic. But I really don't know the correct syntax for this.

    The program asks the user for a filename, opens it, and displays the content. My problem is that I don't know the correct syntax to use ifstream to open a file, with a filename that the user inputs. Basically, how do I tell it to read a file without using quotation marks ?

    I was thinking along the lines of:

    Code:
    ifstream a_file (<<location<< );
    
    // location bieng the string in which the filename & location are stored.
    But the above wouldn't compile.

    Please help.
    Any help (as always) gratefully appriciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Code:
    // suppose filename is a std::string that contains the user input
    ifstream a_file (filename.c_str());
    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

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Oh. Short and simple. The way I like code.
    Cheers Laserlight.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Oh, another quick question:

    Any ideas why I keep getting stuck in this loop ?

    Code:
    ifstream a_file (location.c_str());
        a_file>> content;
        
        if ( a_file.fail() )
        {
            mb_result = MessageBox(NULL, "                             Failed to locate/read a file.\n                              Do you wish to try again ?                                                  ", "   Text.exe", MB_YESNO | MB_ICONEXCLAMATION);
            
            while ( mb_result == IDYES && a_file.fail() )
            {
            
                system("cls");
                
                cout<<"\n    TXT READER\n\n\n    NOTE: This program is used for reading\n          txt documents only.\n\n\n";
        
                cout<<"    Text file you wish to read:\n\n    ";
                getline (cin, location);
        
                ifstream a_file (location.c_str());
                a_file>> content;
                 
            }
        }
    Cheers

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    You need to clear the status of the stream, and you should not create another ifstream of the same name with a local scope.

    Code:
    getline (cin, location);
    
    a_file.clear(); // clear the error status of the ifstream
    a_file.open(location.c_str());
    a_file>> content;
    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

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Ah, the code of a true genius !
    It worked !

    Thanks again !

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Ah, the code of a true genius !
    Not quite, I think it is lacking the code to close the existing stream before you open again. I am not sure of the effects of opening a stream before closing the existing failed stream, but explicitly closing it would be clearer anyway.
    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

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    So I just add an : a_file.close();

    before opening again right ?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Yes.
    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

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Cool, I got summthin' right for a change.

    Thankyou for all your time laserlight. It was most helpful.


    EDIT: Sorry confused again.

    Using that code, how would I tell it to read the whole text file ?
    I noticed it's only reading up to the first space, or first newline.
    Last edited by Necrofear; 06-29-2006 at 02:04 PM.

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    to read a whole text file this is what I do:

    Code:
    ifstream in ( "filename.txt" ); // stream
    
    string WholeFile; // Where I want to store the whole file
    string temp; // just a tempray thing for getting lines.
    
    while ( getline( in, temp) ) // gets a line of the file and stores in temp
    {
        WholeFile += temp; // appends temp to the file string
        WholeFile += "\n"; // appends a new line to the file string
    }
    
    in.close();// closes stream
    or something along those lines. I have it as a function for simplicity.
    Last edited by twomers; 06-29-2006 at 02:17 PM.

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    while(i) s += i.get();
    Or something where s is an std::string, i is the input file. This also works

    Code:
    	std::ifstream i(yrfile);
    	std::ostringstream o; 
    	
    	o << i.rdbuf();
    	std::string s(o.str());
    http://www.cplusplus.com/ref/iostream/ios/rdbuf.html
    http://www.cplusplus.com/ref/iostream/stringstream/

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Sorry, didn't get some of that.
    Could you please go through it with me.

    Thanks

    Oh you changed it. Cheers.

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Me or Tonto? If it's me, I just commented it. I always forget to do that.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Yeah, it was you. Cheers everyone.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined functions
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 02:53 PM
  2. Issues reading text files
    By ozzy34 in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2004, 08:15 AM
  3. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  4. question about reading files in
    By smd in forum C++ Programming
    Replies: 11
    Last Post: 08-25-2003, 07:40 PM
  5. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM

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