Creating a text file in run time

I'm trying to copy the data in one text file to another 'new' text file.
The first text file already exists in the directory of the .cpp file.
The second text file is created by obtaining the name of it in the runtime.
The problem i'm facing is that i can't see the 'new' file that is created.
The same code works in codeblocks IDE(The 'new' second file is created), but in Visual studio it isn't created.
Kindly help me out here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        char dat;
	string wnam;
	ifstream exs;

	exs.open("test.txt",ios::in);

	cout << "Enter the name of the second file\n";
	getline(cin,wnam);

	fstream cre;
	cre.open(wnam.c_str());

	while (exs.get(dat)) {
		cre << dat;
	}
	cout <<"Done!";

	exs.close();
	cre.close();
Last edited on
Where is test.txt located in relation to your executable file?

Because different IDEs have different ideas as to what they set the "current directory" to when running programs.

You should check your exs for errors before trying to use it.
The test.txt file is in the same folder as the .exe file as well.
Do you mean the use of failbit to check for error?
> The test.txt file is in the same folder as the .exe file as well.
Well from memory VS sets the current directory to be your project root directory.
Which might explain why it can't find the file.

I highly recommend that you run programs outside of visual to test them for this reason. Launch the program by double clicking the .exe file or from the command line, and it will behave as expected. Launch it inside visual, and it will bork up the current working path. You can configure VS for each project to do what you want, if you prefer that (that is make it use the working path as if run normally, its doable). I like to run it without VS because if I distribute the program to other people, they won't be launching it from inside VS they will run it directly, so it needs to work in that mode.
Last edited on
1
2
3
cre.open(wnam.c_str(), ios::out);
if(not cre.is_open())
   cerr << "cannot open the file\n";
Thanks @ne555.It works now.
Topic archived. No new replies allowed.