C Program to Write Data to File and Read Data From same File

C Program to Write Data to File and Read Data From same File

This page has a C Program to Write Data to File and Read Data From same File. File may contain a number of records arranged in an order. The File may have a number of records containing same number of fields. All the records in the file may have a unique primary field (Columns). I mean that, the file is just like a table. Each file may contain a unique field called primary key. In addition, the files can be accessable with the help of File Pointers. We can keep and use a file pointer to access records from the file. The below given C program is used to Write Data to a File and Read Data From the same File. Kindly check out the program to Write Data to a File and Read Data From the same File. The questions to write Data to a File and read Data from the same File can be asked as,

1). C program to write Data to a File and read Data from the same File
2). C Program to write Data to one File and read Data from the same File

3). C Program to read Data from File after writing any content unto it.

4). C Program to read Data from File after writing some content unto it


Write Data to one File and read Data from the same File 

  #include<stdio.h>
  int main(){
                FILE *f1;

                             // Structure contains 4 members and initialized for x
                struct Student_Mark{   
                int reg_no;
                char nam[20];
                int total_marks;
                char division[20];
                }x = { 378, "Mr. Put Your Name", 423, "First Class" },y;
                clrscr();
                f1 = fopen("test1.txt", "w");
                fwrite(&x, sizeof(x), 1, f1);
                 // Data written through the structure variable x to the file test1.txt
                fclose(f1);
                f1 = fopen("test1.txt", "r");
                fread(&y, sizeof(y), 1, f1);
                  // Read Data through the structure variable y from the same file
                printf("\nRegister Number : %d", y.reg_no);
                printf("\nName of Student : %s", y.nam);
                printf("\nTotal Marks : %d", y.total_marks);
                printf("\nDivision : %s", y.division);
                fclose(f1);
                getch();
   return (0);
   }


Sample Outputs:

At First, Create a Text file through the same program as above, before to run the program. It is clear that, if you do not create a text file for using it in the program with 'r' mode, the program will not allow to read data from file. After running the program, Verify the Contents of test1.txt file by opening it in Notepad or Ms-word !!


Related Popular Posts:

Other Related C Programs :


* C Program to find whether the given number is Odd or Even

* C Program to find the Sum of Odd Digits

* C Program to find the Sum of Even Digits

* C Program to find the Sum of Even and Odd Digits,

* C Program to Display Odd digits

* C Program to Display even digits

* C Program to Display Odd Numbers,

* C Program to Display Even Numbers,

* C Program to Check whether the given number is Armstrong number

* C Program to display the Armstrong numbers in Range

* C Program to display the Armstrong numbers between 1 and 1000

* C Program to check perfect number, perfect numbers in range

* C Program to Check Prime number, Prime numbers between 1 and 100

* C Program to display the Prime numbers in a Range

* C Program to display the Sum of Prime numbers

* C Program to Check Strong number

* C Program to display the Strong number in Range

* C Program to Check whether the given number is polindrome number or not

* C Program to Check polindrome number using for loop

* C Program to Check polindrome number using Recursion

* C Program to  check whether the given string is polindrome or not

* C Program to find the Roots of Quadratic equation

* C Program to display Fibonacci Series

* C Program to display the Fibonacci Series using an array

* C Program to display the Fibonacci Series using while loop

* C Program to find the Sum of Fibonacci Series, Factorial of Number

* C Program to find the Factorial without using Recursion

* C Program to find the Factorial using Pointers

* C Program to find the Factorial using Function

Bookmark and Share