for me its difficult

A

Aris

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;
}
 
M

Marchello

"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;
}
 
R

Rolf Magnus

Aris said:
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';
}
}
 
R

Ron Natalie

Aris said:
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).
 
A

Aris

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!
 
M

mlimber

Aris said:
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-lite/misc-technical-issues.html#faq-39.1

Cheers! --M
 
P

puzzlecracker

Aris said:
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
 

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
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top