You must Sign In to post a response.
  • C program to write data into a file and read the same data from that file.

    Following is the question asked in BAMU(Babasaheb Ambedkar Marathwada University) B.Sc in Computer Science(Honours) question paper for October 2018 examination.
    This question is asked for 10 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program in c to open file named test.txt, add Name and address of university, close
    the file. Reopen the file and read the file and display the data. [10 Marks]
  • Answers

    1 Answers found.
  • In C programming file handling or file manipulation is one of the important aspect. Here data is inputted through keyboard and it gets displayed on the console(monitor) but actually this data is permanently stored on the disk in the form of file and that is why this approach is called as file handling. Our program run an instruction to open the file which is available on the hard disk so actual input to the program is provided by file and also whatever result is produced that is also kept on hard disk.
    File can be opened in different modes like
    w - write mode
    r - read mode
    a - append mode
    w+ - write and read mode
    r+ - read and write mode
    a+ - append mode
    Here in this program while inserting data into file and while accessing data from file we use w+ and r+ mode respectively.
    After inserting or inputting the data we use instruction fclose() which is actually a standard function, this function write the data on hard disk in the form of file and then destroy the link between your C program and hard disk. Similarly after reading the file completely we use fclose() so as to destroy the relationship between your C program and hard disk.

    Please insert "stdio.h" and "conio.h" header files before declaration of main function.
    void main(void)
    FILE * fp;
    char ch;
    fp= fopen("test.txt","w+");
    printf("Enter university name and address\n");
    printf("\nYou have to press ctrl + z keys to stop inputting\n");
    printf("\nData stored in the file data.txt is as follows\n");

    output of this program is available with attached file.


    Delete Attachment

  • Sign In to post your comments