Opening a textfile with user supplied filename

This is a discussion on Opening a textfile with user supplied filename within the C++ Programming forums, part of the General Programming Boards category; I am writing a simple text file manipulation program and am getting the following errors from gcc: file converter.cpp: In ...

  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
    21,461
    Compile with g++, not gcc, unless you want to explicitly specify to link with the C++ standard library.
    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

  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, 09: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

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