Write a program that prompts the user to enter a source file
Write a program that prompts the user to enter a source file and a target file. The program copies the content in the source file to the target file. Make sure that the source file exists.
You can have any content in the input file.
Hints: Use get and put.
Solution
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<process.h>
void main()
{
fstream file1, file2;
char ch;
clrscr();
file1.open(\"file1.txt\",ios :: out);
cout << \"Enter the data\";
cin.get(ch);
while(ch != \'n\')
{
file1.put(ch);
cin.get(ch);
}
file1.close();
file1.open(\"file1.txt\",ios :: in);
if(!file1)
{
cout<< \"n File1 not found !! n\";
getch();
exit(0); // aborting…
}
cout << \"n First file contents n\";
while(file1)
{
file1.get(ch);
cout.put(ch);
}
file1.seekg(0); /* copying process..... */
file2.open(\"file2.txt\",ios :: out );
cout << \" First file contents copied to second file...\";
while(file1)
{
file1.get(ch);
file2.put(ch);
}
file1.seekg(0);
file2.close();
file2.open(\"file1.txt\",ios :: in);
if(!file1)
{
cout<< \"n File1 not found !! n\";
getch();
exit(0); // aborting…
}
cout << \"n Second file contents n\";
while(file2)
{
file2.get(ch);
cout.put(ch);
}
file2.close();
getch();
}
| #include<iostream.h> #include<conio.h> #include<fstream.h> #include<string.h> #include<process.h> void main() { fstream file1, file2; char ch; clrscr(); file1.open(\"file1.txt\",ios :: out); cout << \"Enter the data\"; cin.get(ch); while(ch != \'n\') { file1.put(ch); cin.get(ch); } file1.close(); file1.open(\"file1.txt\",ios :: in); if(!file1) { cout<< \"n File1 not found !! n\"; getch(); exit(0); // aborting… } cout << \"n First file contents n\"; while(file1) { file1.get(ch); cout.put(ch); } file1.seekg(0); /* copying process..... */ file2.open(\"file2.txt\",ios :: out ); cout << \" First file contents copied to second file...\"; while(file1) { file1.get(ch); file2.put(ch); } file1.seekg(0); file2.close(); file2.open(\"file1.txt\",ios :: in); if(!file1) { cout<< \"n File1 not found !! n\"; getch(); exit(0); // aborting… } cout << \"n Second file contents n\"; while(file2) { file2.get(ch); cout.put(ch); } file2.close(); getch(); } |



