Thread: Using a variable for a filename

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Using a variable for a filename

    i seem to be having trouble using a variable as a file name for an ifstream.
    i am making a program that is quite simple. all it does is ask for a filename, reads the file and displays it to the screen. my source code is
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        string file;
        char letter;
        ifstream in_stream;
        cout << "Please enter the name of \n"
                "the file you wish to open. \n"
                "Please use only text files. \n";
        cin >> file;
        in_stream.open(file);
        if (in_stream.fail())
        {
                    cout << "File Failed To Open";
                    system("pause");
                    exit(1);
                    }
        do 
        {
                    in_stream.get(letter);
                    cout.put(letter);
                    }
        while(! in_stream.eof());
        cout << endl;
        system("pause");
    }
    the underlined line is where i receive errors. I'm sure that I should be able to do this. Im following the syntax out of a book. Can someone tell me whats wrong?

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    ifstream::open takes a character array as the first parameter, not a string. Either read in the filename as a char array or convert it to one.
    Last edited by Brad0407; 12-14-2007 at 11:25 PM. Reason: disable smilies
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    which can be done via

    Code:
    in_stream.open(file.c_str());

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Or even better, don't create the ifstream until you're actually ready to use it and then use the constructor instead of open()
    Code:
        string file;
        cout << "Please enter the name of \n"
                "the file you wish to open. \n"
                "Please use only text files. \n";
        cin >> file;
        ifstream in_stream( file.c_str() );
        if ( !in_stream )

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    thank you. my program is fine now. I changed the problem part to
    Code:
        in_stream.open(file.c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  2. I/O--Using a variable as a filename
    By jedo in forum C++ Programming
    Replies: 7
    Last Post: 09-06-2005, 10:57 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM