Why do we need data files? What are the different file opening modes? Write a program that reads data from a file “input.txt” and writes to “output.txt” file.

This answer is restricted. Please login to view the answer of this question.

Login Now

A data file is a computer file which stores data to be used by a computer application or system, including input and output data. A data file usually does not contain or code to be executed (that is, a computer program). Need of data files are listed below:

  • When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates.
  • If we have to enter a large number of data, it will take a lot of time to enter them all.
  • However, if we have a file containing all the data, we can easily access the contents of the file using a few commands in C.
  • We can easily move our data from one computer to another without changes.

Openings Modes in Standard I/O

Mode Meaning of Mode During Inexistence of file
R Open for reading If the file does not exist, fopen() returns NULL
Rb Open for reading in binary mode If the file does not exist, fopen() returns NULL
W Open for reading If the file exists, its contents are overwritten. If the file does not exist, it will created
Wb  Open for writing in binary mode If the file exists, its contents are overwritten. If the file does not exist, it will created
A Open for append. Data is added to the end of the file. If the file does not exist, if will be created.
Ab Open for append in binary mode. Data is added to the end of the file. If the file does not exist, it will be created
r+ Open for both reading and writing. If the file does not exist, fopen() returns NULL.
rb+ Open for both reading and writing in binary mode. If the file does not exist, fopen() returns NULL.
w+ Open for both reading and writing If the file exists, its contents are overwritten. If the file does not exist, it will be created.
wb+ Open for both reading and writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
a+ Open for both reading and appending. If the file does not exist, it will be created.
ab+ Open for both reading and appending in binary mode. If the file does not exist, it will be created.

Program Part

#include <stdio.h>
#include <stdlib.h>

int main(){

    FILE *fptr1, *fptr2; 
    char c;

    // Open One file for reading 
    fptrl = fopen("input.txt", "r");
    
    if( fptrl == NULL ){
        printf("Cannot open a file");
        exit(0);
    }

    //opeaning another file for writing
    fptr2 = fopen("output.txt", "w");
    if( fptr2 == NULL ){
        printf("Cannot open a file");
        exit(0);
    }

    //Read contents from file
    c = fgets(fptr1);

    while(c != EOF){
        fputc(c, fptr2);
        c = fgetc(fptr1)''
    }

    printf("\nContents copied");
    fclose(fptr1);
    fclose(fptr2);
    
    return 0;

}
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .