#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14

    Problem creating output file with variable and string


    I'm getting stuck on something which I consider rather simple, yet am not sure how to solve it!

    In my int main, I'm using argv to allow command line arguments.

    These arguments are being used for doing some input/output file processing.

    I've successfully used the input method to read in a file i.e.:

    ifstream inFile (argv[1]);


    However, I'm now upto to creating and writing an output file and this is where my problem lies. What essentially I want to do is to create an output file with the same name as the input file but with an additional ending(".output"). I've used the following code:

    ofstream outFile (argv[1] + ".output"); // Line 168

    When I try to compile this program I get the following error message refering to the line of code shown above:

    task4dr.cc: In function `int main(int, char**)':
    task4dr.cc:168: invalid operands of types `char*' and `const char[8]' to
    binary `operator+'

    I don't see why it won't let me concatenate a variable with a string although this seems to be the problem.

    Any ideas?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,143
    Rep Power
    2222
    argv[1] is of type char* . I assume that the "+" operator is for the type string -- I'm also assuming that your intention is to use the string type. STL is after my time, but I'm fairly sure that char* and string are two different types and that there is no "+" operator defined for char* .

    I would create a string variable and assign argv[1] to it. Then I would concatenate the ".output" extension to it, either before the instantiation of outFile or within it as you are now doing. And I would use that string variable in the instantiation of outFile.
    Last edited by dwise1_aol; April 17th, 2003 at 09:37 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14
    I've just tried that by doing the following:

    string strarv1 = argv[1];
    string outputfile = strarv1 + ".report";

    ofstream outFile (outputfile); // Line 171

    Although this appears to have sorted out the error shown in the previous post, I now have another error referring to line 171:

    task4dr.cc: In function `int main(int, char**)':
    task4dr.cc:171: no matching function for call to `std::basic_ofstream<char,
    std::char_traits<char> >::basic_ofstream(std::string&)'
    /usr/local/include/c++/3.1.1/iosfwd:89: candidates are:
    std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const
    std::basic_ofstream<char, std::char_traits<char> >&)
    /usr/local/include/c++/3.1.1/fstream:408:
    std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*,
    std::_Ios_Openmode = (std::ios_base::out | std::ios_base::trunc)) [with
    _CharT = char, _Traits = std::char_traits<char>]
    /usr/local/include/c++/3.1.1/fstream:394:
    std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char,
    _Traits = std::char_traits<char>]

    Any ideas what this error message may mean in this particular context?
    Last edited by markb_1984; April 17th, 2003 at 09:53 AM.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,143
    Rep Power
    2222
    I'm going to have to learn STL one of these days. I don't know how much longer I can put it off.

    I just Google'd on - STL string - and found this on the first hit:

    c_str
    const char* c_str() const;
    For compatibility with "older" code, including some C++ library routines, it is sometimes necessary to convert a string object into a character array ("C-style string"). This function does the conversion. For example, you might open a file stream with a user-specified file name:
    string filename;
    cout << "Enter file name: ";
    cin >> filename;
    ofstream outfile (filename.c_str());
    outfile << "Data" << endl;

    at "ANSI String Class" by Dr. Mark J. Sebern, http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm

    Apparently, the ofstream constructor cannot handle STL.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14
    That seems to have sorted it. I think i'll go away and do some research to get my head around the mechanics of this, thanks!

IMN logo majestic logo threadwatch logo seochat tools logo