Usage of strcat for output file name..


I have user defined file name! Therefore, i wanted to use the input name for my export file too.


Here below my reading and writing function
Here fname is defined by user! Example when the user define the input name as 'NAME' so the program read NAME.txt, this works well,
But when i am trying to use the input name 'NAME' for my export file, i got i problem with strcat,
For example, I want like 'NAME_out.txt', but now i am getting 'NAME.txt_out.txt' even if i use NAME further, it will add on the previous one, like NAME.txt_out.txt_out_txt and so on

I always want to keep NAME_out.txt



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void Getopt_training::read_file(char* fname)
{
	   double line;
		fstream file;
		strcat(fname, ".txt");
	    file.open(fname);

	    if (!file)
		{
		cout << "Could not open the file. Improve.\n";

		}
		else
		{

		int count = 10;
		while (file>>line)
		{
		cout << line << '\n';
		line2.push_back(line);
		count--;
		}
		file.close();
		}
}

void Getopt_training::write_file(char* filename)
{
	ofstream myfile;
	strcat(filename, "_snappy.txt");
	myfile.open(filename);

if (!myfile)
{
	cout <<"Couldn't write the file:"<<endl;
}
else
{
	int kk =line2.size();
	for (int i=0; i < kk; i++)
		{
		myfile<<line2[i]<<endl;
		}
}
}



Any help
Topic archived. No new replies allowed.