Thread: C++ noob help with strings

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    8

    C++ noob help with strings

    Hi everyone, new here. I have a class in C++ and even my teacher can't figure this one out, so maybe someone here will spot something?

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
    
    int main()
    {
    string userName;
    ofstream outputFile;
    
    cout << "Please enter a file name to open." << endl;
    cin >> userName;
    outputFile.open(userName);
    cout << fileName << " is opened successfully!" << endl;
    return 0;
    }
    my error is
    Code:
    1>------ Build started: Project: debug, Configuration: Debug Win32 ------
    1>Compiling...
    1>debug.cpp
    1>c:\users\skyler\documents\visual studio 2008\projects\debug\debug\debug.cpp(13) : error C2664: 'void std::basic_ofstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    1>c:\users\skyler\documents\visual studio 2008\projects\debug\debug\debug.cpp(14) : error C2065: 'fileName' : undeclared identifier
    1>Build log was saved at "file://c:\Users\Skyler\Documents\Visual Studio 2008\Projects\debug\debug\Debug\BuildLog.htm"
    1>debug - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    please help!

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    If there is no way for you to specify this is an ANSI project, add the following line at the top of your file:
    Code:
    #undef UNICODE
    Edit: Alternatively you can just use wstring as well.

    Edit: Nevermind my post, syzygy is right.
    Last edited by Desolation; 04-26-2010 at 07:03 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    The open functions wants a char * not a string. You may have problems with wide chars if it's asking for wchar_t, but try

    Code:
    outputFile.open( userName.c_str() );

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    THANKS!

    but that adds the question

    would
    Code:
    outputFile.open(userName.data());
    be the same as c_str?

    I just found it in a chapter near the end of my book

    if they are different, whats that difference?

    BTW:My teacher will be thrilled that someone found the answer!

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your teacher is (hopefully) forcing you to learn by professing ignorance. Any teacher of C++ who does not know the answer to this question should not be teaching C++.

    The fix is simple.
    Code:
    outputFile.open(userName.c_str());
    C++ supports more than one type of string representation. One is the std::string class, which you are using. Another is an array of chars terminated with a zero (so "Hello" is represented as an array of 6 char with values 'H', 'e', 'l', 'l', 'o', and zero.).

    ofstream's open() method accepts a string as an array of chars. There is no implicit conversion from a std::string to an array of char (hence the error message you are seeing). std::string's c_str() method provides a string in the form of an array of char.
    Last edited by grumpy; 04-26-2010 at 07:16 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    Quote Originally Posted by grumpy View Post
    Your teacher's skills clearly do not include C++.

    The fix is simple.
    Code:
    outputFile.open(userName.c_str());
    C++ supports more than one types of string representation. One is the std::string class, which you are using. Another is an array of chars terminated with a zero (so "Hello" is represented as an array of 6 char with values 'H', 'e', 'l', 'l', 'o', and zero.).

    ofstream's open() method accepts a string as an array of chars. There is no implicit conversion from a std::string to an array of char (hence the error message you are seeing). std::string's c_str() method provides a string in the form of an array of char.
    LoL! I think he's more C than C++, and from what I understand there's quite a bit of difference, as much as whats similar?

    But he works for Lockheed Martin doing internal software programming, He talks about C alot, I assume that is what he's fluent in. Other than this though, he seems to know what he's talking about...I hope...

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815

    !

    Ah. A C programmer teaching C++.

    Sort of like a german teaching italian, never having being taught italian himself. Sure, some things in common between the languages, but students will speak italian with a very weird accent.

    Ach tung!
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    HAHA!

    So...IS ther any difference between using .c_str or .data()? to pull the c-string out of a string? (am I using correct terminology?)

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> IS ther any difference between using .c_str or .data()

    Yes. c_str() will always work there, data() will only sometimes work and sometimes not and you won't know for sure when it will work and when it will fail.

    The reason is that data() doesn't necessarily make sure that there is the "terminating null" or terminating zero character mentioned by grumpy. So the open function might think the filename is still going and keep looking for more letters until it doesn't find that terminating character. D:\myfile.txt might end up looking like D:\myfile.txt2b@æ$fu~

    data() isn't meant to pull a C string out of a C++ string (your terminology is fine). It is meant to just get the characters in the string. That's why it doesn't guarantee the last null character that is required for a C style string.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    You guys rock! the careful explanations and timely responses!
    I am so hanging out here!

    Thank you all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. help with fopen() and strings. i'm a noob.
    By xhoangx in forum C Programming
    Replies: 3
    Last Post: 12-04-2005, 08:40 PM
  4. Noob trouble: returning strings (pointers to char)
    By SgtMuffles in forum C Programming
    Replies: 4
    Last Post: 02-28-2005, 08:48 PM
  5. Noob having trouble storing strings
    By wiramu in forum C++ Programming
    Replies: 6
    Last Post: 12-26-2003, 06:22 AM