1. Write a Program in C++ to copy the contents of the file from one location to another location (Ex: C: \Demo \Test.txt to D: \Demo \Test.txt) and also check the content in both files are same or not.
2. Write a program in C++ to enter a string into file and read the same file to display all the words in the file with stating letter ‘p’.
1. Write a Program in C++ to copy the contents of the file from one location to another location (Ex: C: \Demo \Test.txt to D: \Demo \Test.txt) and also check the content in both files are same or not.
#include<iostream>
using namespace std;
int main()
{
char ch, sFile[20], tFile[20];
FILE *fs, *ft;
cout<<"ENTER SOURCE FILE NAME (Include correct file extension e.g mydocument.docs ): ";
cin>>sFile;
fs = fopen(sFile, "r");
if(fs == NULL)
{
cout<<"\nERROR!!!, TRY AGAIN";
return 0;
}
cout<<"\nENTER TARGET FILE NAME (Include correct file extension e.g mydocument.docs): ";
cin>>tFile;
ft = fopen(tFile, "w");
if(ft == NULL)
{
cout<<"\nError Occurred!";
return 0;
}
ch = fgetc(fs);
while(ch != EOF)
{
fputc(ch, ft);
ch = fgetc(fs);
}
cout<<"\nFILE COPIED SUCCESSFULLY.";
fclose(fs);
fclose(ft);
cout<<endl;
return 0;
}
2. Write a program in C++ to enter a string into file and read the same file to display all the words in the file with stating letter ‘p’.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
int a, b;
// writting to the file
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
// reading from the file
ifstream bd;
myfile.open("file.txt");
if (myfile.is_open())
while (bd >> a >> b)
cout << a << b << endl;
else cout << "ERROR";
return 0;
}
Comments
Leave a comment