 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
merrittr Guest
|
Posted: Mon Nov 13, 2006 10:10 am Post subject: trying to concat a filename (newbie) |
|
|
I was trying to build a list of input files using users input
ie #>program 1 2 3 4 5 6 7 8 9 10 11 12
in the program I wanted name generated input filenames
char beginning[64]="count",endding[64]=".txt", outf[64]="data";
char cf1[64]=strcat(strcat(beginning,argv[0]),endding);
//sprintf (cf1, "%s%s%s",beginning,argv[0],endding);
ifstream in1 (cf1);
what I get at gcc time:
ptcllocate.cpp:16: error: invalid initializer
as you can see I tried sprintf too (after looking in this group) any
hints
on how I can get this to work? |
|
| Back to top |
|
 |
benben Guest
|
Posted: Mon Nov 13, 2006 10:10 am Post subject: Re: trying to concat a filename (newbie) |
|
|
merrittr wrote:
| Quote: | I was trying to build a list of input files using users input
ie #>program 1 2 3 4 5 6 7 8 9 10 11 12
in the program I wanted name generated input filenames
char beginning[64]="count",endding[64]=".txt", outf[64]="data";
|
Should have been:
const char[] beginning = "count";
const char[] endding = ".txt";
const char[] outf = "data";
| Quote: | char cf1[64]=strcat(strcat(beginning,argv[0]),endding);
|
strcat returns a pointer (which points to the first element of
beginning, in this case.) However, you cannot initiate a char[64] array
with a pointer. It just doesn't make sense.
A clearer way to do it is to be explicit about the buffer:
char buff[128]; // fixed size buffer can overflow
strcat(buff, beginning);
strcat(buff, argv[0]);
strcat(buff, endding);
Google on strcat if you are not sure how it should be used.
| Quote: | //sprintf (cf1, "%s%s%s",beginning,argv[0],endding);
|
There are much better alternatives, such as using the standard library
string:
std::string beginning = "count";
std::string endding = ".txt";
std::string str = beginning + argv[0] + endding;
or a string stream:
std::ostringstream oss;
oss << "count" << argv[0] << ".txt";
std::string str = oss.str();
Just avoid writing C in C++.
Regards,
Ben |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Mon Nov 13, 2006 10:10 am Post subject: Re: trying to concat a filename (newbie) |
|
|
merrittr wrote:
| Quote: | I was trying to build a list of input files using users input
ie #>program 1 2 3 4 5 6 7 8 9 10 11 12
in the program I wanted name generated input filenames
char beginning[64]="count",endding[64]=".txt", outf[64]="data";
char cf1[64]=strcat(strcat(beginning,argv[0]),endding);
//sprintf (cf1, "%s%s%s",beginning,argv[0],endding);
ifstream in1 (cf1);
what I get at gcc time:
ptcllocate.cpp:16: error: invalid initializer
as you can see I tried sprintf too (after looking in this group) any
hints
on how I can get this to work?
|
Why not use a std::vector of std::strings instead.
And usually, argv[0] is the program's path + executable name.
So you need to collect all argv[]s except the first and the program
should work for any number of arguements.
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
template< typename T >
void show_parameters(std::vector< T >& r_v)
{
std::copy( r_v.begin(),
r_v.end(),
std::ostream_iterator< T >(std::cout, "\n") );
}
int main(int argc, char* argv[])
{
const size_t Size = static_cast<size_t>(argc); // cvt to size_t
std::vector< std::string > vargs(Size - 1); // container, presized
const std::string s_count("count"), s_extension(".txt"); // const s
for( size_t i = 0; i < vargs.size(); ++i)
{
vargs[i] = s_count + argv[i + 1] + s_extension;
}
show_parameters< std::string >( vargs );
}
/* program 1 2 3 4 5
count1.txt
count2.txt
count3.txt
count4.txt
count5.txt
*/ |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|