C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 17th, 2003, 06:19 AM
markb_1984 markb_1984 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 137 markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 53 m 52 sec
Reputation Power: 9
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?

Reply With Quote
  #2  
Old April 17th, 2003, 09:34 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
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.

Reply With Quote
  #3  
Old April 17th, 2003, 09:46 AM
markb_1984 markb_1984 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 137 markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 53 m 52 sec
Reputation Power: 9
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:ut | 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.

Reply With Quote
  #4  
Old April 17th, 2003, 01:19 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
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.

Reply With Quote
  #5  
Old April 17th, 2003, 02:09 PM
markb_1984 markb_1984 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 137 markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level)markb_1984 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 53 m 52 sec
Reputation Power: 9
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Problem creating output file with variable and string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway