CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2009
    Posts
    14

    C++ File IO Question

    The code below attempts to read in a file. It first asks the user to enter the name of the file.

    Here is the code:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream> //file io support
    #include <cstdlib> //exit() support

    int main(void)
    {
    using namespace std;

    string data_in = "";
    string filename = "";

    cout << "Enter filename: ";
    getline(cin,filename);

    ifstream file_in (filename); //create input file type
    if(file_in.is_open()) //if the file does not open properly
    {
    while(! file_in.eof())
    {
    getline(file_in,data_in);
    cout << endl << data_in;
    }
    file_in.close();
    }
    else
    {
    cout << "Unable to open file";
    }

    return 0;
    }

    The problem I am having is the compiler is complaining about the following line:
    ifstream file_in (filename);

    If I replace this line with the following, all is well:
    ifstream file_in ("filename.txt");

    Why is this? Why can't I use a variable name here? How can I get this to work?

  2. #2
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: C++ File IO Question

    You need to convert your string object to a char array (cstring) by calling c_str() on your filename object.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  3. #3
    Join Date
    Apr 2009
    Posts
    14

    Re: C++ File IO Question

    So the ifstream class only supports a char type?

  4. #4
    Join Date
    Apr 2009
    Posts
    14

    Re: C++ File IO Question

    Isn't "filename.txt" a string? Why does this work?

  5. #5
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: C++ File IO Question

    Quote Originally Posted by westco View Post
    Isn't "filename.txt" a string? Why does this work?
    No, "filename.txt" in not a string; it is an array of char primitives. Above when you created a string and assigned it the filename, what is happening is behind the scenes, the string object is converting that array of chars into a string object representation of the same data. Latter, we may need to convert it back, so the c_str() method is provided
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  6. #6
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: C++ File IO Question

    Quote Originally Posted by westco View Post
    So the ifstream class only supports a char type?
    Quote Originally Posted by www.cplusplus.com
    ifstream ( );
    explicit ifstream ( const char * filename, ios_base:: openmode mode = ios_base::in );
    It appears so
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  7. #7
    Join Date
    Apr 2009
    Posts
    14

    Re: C++ File IO Question

    Thanks Etherous.

    I think I understand now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured