trying to concat a filename (newbie)

M

merrittr

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?
 
S

Salt_Peter

merrittr said:
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::eek:stream_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 = 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
*/
 
B

benben

merrittr said:
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";
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.
//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::eek:stringstream oss;
oss << "count" << argv[0] << ".txt";

std::string str = oss.str();

Just avoid writing C in C++.

Regards,
Ben
 
M

Michiel.Salters

Salt_Peter schreef:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
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 = s_count + argv[i + 1] + s_extension;
}


Wouldn't it be easier to first copy the args, then convert them?

i.e.

int main(int argc, char* argv[])
{
std::vector< std::string > vargs(argv, argv+argc);
std::for_each(vargs.begin()+1, vargs.end(), &foo);
// ...
}

HTH,
Michiel Salters
 
M

merrittr

thanks guys , using std::string I think Will do the trick




Salt_Peter schreef:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
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 = s_count + argv[i + 1] + s_extension;
}


Wouldn't it be easier to first copy the args, then convert them?

i.e.

int main(int argc, char* argv[])
{
std::vector< std::string > vargs(argv, argv+argc);
std::for_each(vargs.begin()+1, vargs.end(), &foo);
// ...
}

HTH,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top