Q-15: Write a C/C++ program that performs read and write operations in a file.
    

Read Using- C


#include<stdio.h> #include<stdlib.h> void main() { char fileName[100], line[1024]; FILE *fptr; printf("Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory): "); gets(fileName); if((fptr = fopen(fileName,"r")) == NULL) { printf("Error! Opening File!"); exit(1); } printf("\n"); while(fgets(line, sizeof(line), fptr)) { printf("%s", line); } fclose(fptr); }

OUTPUT

Case-1
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory):  readThis.txt

Welcome GCC x CSD! A hub for the students of CS Department of GC College, Silchar


Case-2 ---------------------------------------------------------------------------------------------------------------------------------------------------------------- Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory): E:\\Read and Write\\gccxcsd.txt Hey Anuj here! How are you?
  

Write Using - C


#include<stdio.h> #include<stdlib.h> void main() { char fileName[1024], input_str[1024]; FILE *fptr; printf("Provide Path(c:\\..) or Enter File Name with Extension(if present in the same directory): "); gets(fileName); if((fptr = fopen(fileName,"w")) == NULL) { printf("Error!"); exit(1); } printf("Enter Strings to Write into File: "); gets(input_str); fprintf(fptr,"%s",input_str); fclose(fptr); printf("\nSuccessfully written to %s",fileName); }

OUTPUT

Case-1
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory):  write.txt
Enter Strings to Write into File:  Hello, How are you guys??

Successfully written to write.txt


Case-2 ---------------------------------------------------------------------------------------------------------------------------------------------------------------- Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory): E:\\Read and Write\\gccxcsd.txt Enter Strings to Write into File: Welcome to GCC x CSD !! Successfully written to E:\\Read and Write\\gccxcsd.txt
        

Read Using - C++


#include<iostream> #include<fstream> using namespace std; int main() { char fileName[1024], output_str[1024]; cout<< "Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory): "; cin.getline(fileName,1024); ifstream MyFile(fileName); while(MyFile.eof()==0) { MyFile.getline(output_str,1024); cout<< endl<< output_str; } MyFile.close(); return 0; }

OUTPUT

Case-1
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory):  write.txt

Hello, How are you guys??


Case-2 ---------------------------------------------------------------------------------------------------------------------------------------------------------------- Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory): E:\\Read and Write\\gccxcsd.txt Welcome to GCC x CSD !!
  

write Using- C++


#include<iostream> #include<fstream> using namespace std; int main() { char fileName[1024], input_str[1024]; cout<< "Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory): "; cin.getline(fileName,1024); ofstream MyFile(fileName); cout<< "Enter Strings to Write into File: "; cin.getline(input_str,1024); MyFile << input_str; MyFile.close(); cout<< "\nSuccessfully written to "<< fileName; return 0; }

OUTPUT

Case-1
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory):  write.txt
Enter Strings to Write into File:  Please! Support this Website
  
Successfully written to write.txt


Case-2 ---------------------------------------------------------------------------------------------------------------------------------------------------------------- Provide Path(C:\\..) or Enter File Name with Extension(if present in the same directory): E:\\Read and Write\\gccxcsd.txt Enter Strings to Write into File: You can contribute here too! Successfully written to E:\\Read and Write\\gccxcsd.txt