create a .txt file by open it in write mode then copy same file in onother .txt file

Hi experts ,

I want create a txt file and write some lines to it then copy this file to onother file ,

I do this in this way but secound part (create onother file and copy first file ino it ) is not working

I am a bigginer in programming please help me !!!

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
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<conio.h>
#include<fstream>
#include<iostream>

using namespace std;

int main()
{
	ofstream out("D:\\test.txt");
	if( out.fail( ) )
	{
		cerr<< " can not open the file ";
	
		_getch();
		return 1;
	}
	out<<123<<" "<<123.23<<endl;
	out<<"This is a text file . ";
	out.close();
	

	fstream in("D:\\test.txt");	
	char ch1,ch2;
	in.open("D:\\test.txt");
	
	if( in.fail( ) )
	{
		cerr<< " No such a file Exist";
		getch();
		return 1;
	}

	ofstream output ("E:\\test file.txt");
	output.open("E:\\test file.txt");
	if(output.fail( ))
	{
		cerr<<"Unable to create a file";
		getch();
		return 1;
	}

	while( !in.eof( ) )
	{
		ch1 = (char) in.get( );
		output.put(ch2);
	}

	output.close();
	in.close();

	_getch();
	return 0;
}


output of this code is : No such file a Exist

thanks a lot in advanced .
Does the file exist? If it does, you only need to open it once.

fstream in("D:\\test.txt"); opens it.
thanks
yes this is exist

but problem is here (I solved myself ;-) :

1
2
ofstream output ("E:\\test file.txt");
	output.open("E:\\test file.txt");


that should change to :

1
2
ofstream output ;
output.open("E:\\test file.txt");


and same way ,
1
2
fstream in("D:\\test.txt");
in.open("D:\\test.txt");


should be
1
2
fstream in;
in.open("D:\\test.txt");
Last edited on
Topic archived. No new replies allowed.