By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,382 Members | 1,567 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,382 IT Pros & Developers. It's quick & easy.

for me its difficult

P: n/a
Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","file2.txt"....."file5.txt"
But instead I get crazy things like theese:
file1.txt
file1.txt2
file1.txt23
file1.txt233@
can you help me?

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char * Create_File_Name(int counter_files)
{
char *ch1="file";
char *ch2=".txt";
char string[2];
itoa(counter_files,string,10);
char *ch3=strcat(ch1,string);
char *fname=strcat(ch3,".txt");
return fname;
}
int main()
{

char* fname;
for(int i=1; i<5; i++)
{
fname=Create_File_Name(i);
cout<<fname<<endl;
}
int p;
cin>>p;
return 0;
}


Dec 27 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> "file1.txt","file2.txt"....."file5.txt"

void CreateName(char *dest_fname, const char *fname, const char *ext, int
counter)
{
sprintf(dest_fname, "%s%d%s", fname, counter, ext);
}

int main()
{
char fname[] = "file";
char ext[] = ".txt";

char out[64] = {'\0'};

CreateName(out, fname, ext, 1);
cout << out; // here you get "file1.txt"

return 1;
}
Dec 27 '05 #2

P: n/a
Aris wrote:
Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","file2.txt"....."file5.txt"
But instead I get crazy things like theese:
file1.txt
file1.txt2
file1.txt23
file1.txt233@
can you help me?

#include <iostream.h>
This is an outdated non-standard header.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char * Create_File_Name(int counter_files)
{
char *ch1="file";
char *ch2=".txt";
Those should be const char* or const char[].
char string[2];
This means you only have space for one single digit, so you have to ensure
that counter_files is not larger than 9.
itoa(counter_files,string,10);
char *ch3=strcat(ch1,string);
This invokes undefined behavior, because you try to attach something to a
string literal. For one, a string literal is constant and second, you write
beyond its end.
char *fname=strcat(ch3,".txt");
return fname;
}
int main()
{

char* fname;
for(int i=1; i<5; i++)
{
fname=Create_File_Name(i);
cout<<fname<<endl;
}
int p;
cin>>p;
return 0;
}


You should use std::string and std::stringstream. Try this:

#include <iostream>
#include <sstream>
#include <string>

std::string Create_File_Name(int counter_files)
{
std::stringstream stream;
stream << "file" << counter_files << ".txt";
return stream.str();
}

int main()
{
std::string fname;
for (int i = 1; i < 5; ++i)
{
fname = Create_File_Name(i);
std::cout << fname << '\n';
}
}

Dec 27 '05 #3

P: n/a
Aris wrote:
char *ch1="file";
char *ch2=".txt";
char string[2];


char is a single character. char* is a pointer
to a single character. The C++ string type is called
string. Perhaps you should research that (stringstream
might also be useful to you).
Dec 27 '05 #4

P: n/a
thank you,
to all of you!
The problem solved
I come out of a bad situation
and I learned some things more
Merry Christmas to all of you!
Dec 27 '05 #5

P: n/a
Aris wrote:
Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","file2.txt"....."file5.txt"
But instead I get crazy things like theese:
file1.txt
file1.txt2
file1.txt23
file1.txt233@
can you help me?

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char * Create_File_Name(int counter_files)
{
char *ch1="file";
char *ch2=".txt";
char string[2];
itoa(counter_files,string,10);
char *ch3=strcat(ch1,string);
char *fname=strcat(ch3,".txt");
return fname;
}
int main()
{

char* fname;
for(int i=1; i<5; i++)
{
fname=Create_File_Name(i);
cout<<fname<<endl;
}
int p;
cin>>p;
return 0;
}


Don't use character arrays or itoa. Prefer std::string and the
conversion method discussed here:

http://www.parashift.com/c++-faq-lit....html#faq-39.1

Cheers! --M

Dec 27 '05 #6

P: n/a

Aris wrote:
Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","file2.txt"....."file5.txt"


DO NOT USE C++; write a script

Dec 27 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.