Thread: Opening a textfile with user supplied filename

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    Question Opening a textfile with user supplied filename

    I am writing a simple text file manipulation program and am getting the following errors from gcc:

    file converter.cpp: In function ‘int main()’:
    file converter.cpp:32: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string*)’

    line 32 in my program is:

    ifstream inf(startdate);

    where startdate is a string class.

    file converter.cpp:39: error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&, const std::_Ios_Openmode&)’

    line 39 in my program is:

    ofstream outf(asxcode, ios::app);

    Where asxcode is a string class.


    If passing a string is illegal how do I use a user supplied filename?

    Thanks,
    BeerGut

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you are correct that both ifstream and ofstream does not accept type "string" in the constructor, it must be a "c-style string". you can use the "c_str" function, ie "mystring.c_str()" to get the c-string of the given "string".

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    Unhappy Thanks

    Ok I did that, now I'm getting all sorts of complaints about undefined references to string, stringstream, ostream etc.

    At the end gcc gives me:

    collect2: ld returned 1 exit status

    Any ideas whats going wrong?

    Thanks,
    BeerGut

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compile with g++, not gcc, unless you want to explicitly specify to link with the C++ standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    Wink Thanks for the advice

    Thanks it compiles.

    Now I have some programming errors to rectify.
    I'm getting somewhere now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. user defined path for FILENAME
    By drdigimon in forum C Programming
    Replies: 5
    Last Post: 10-12-2003, 06:04 PM
  2. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 06:37 AM
  3. letting the user type in the filename.
    By doampofo in forum C++ Programming
    Replies: 4
    Last Post: 04-16-2003, 09:29 PM
  4. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM