Filing in C (part 2)

Submitted by: 
Language: 

Writing in a File

In this part you will learn:
1. C syntax
2. Filing
3. Writing to a File

In this tutorial we will learn about filing in C. Filing is a very important tool in C. In this program we will write data in a file. We will take input from the user and write that data in a file whose name will be entered by the user.
Writing in a File
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<stdio.h>
  2. #include<conio.h>

These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.

  1. struct student
  2. {
  3. int age;
  4. int gpa;
  5. char name[25];
  6. };
  7.  
  8. int main()
  9. {
  10. char filename[25];
  11. student s;
  12.  
  13. printf("Enter the name of the file: ");
  14. scanf("%s",&filename);
  15.  
  16. printf("Enter you name:");
  17. scanf(" %s",&s.name);
  18. printf("Enter you age:");
  19. scanf(" %d",&s.age);
  20. printf("Enter you GPA:");
  21. scanf(" %d",&s.gpa);
  22.  
  23.  
  24. FILE *fp=fopen(filename,"w");

In this program we are writing a student record in the file. At first we declared a structure. In this structure we declared three variables. A character array for name, an integer for gpa and an integer for age. Then we came in our main function where we declared a character array to store the name of the file that we will take from the user. After taking input from the user we ask the user to input the name , age and gpa.
After taking all the inputs we declared a FILE pointer and assigned it the address of our file. Note that here we wrote “W” after the filename. This “w” means we are opening this file in the write mode.

  1. if(fp==NULL)
  2. {
  3. printf("\nFile cannot be opened.");
  4. }
  5. else
  6. {
  7.  
  8.  
  9.  
  10. fprintf(fp,"Name:%s",s.name);
  11. fprintf(fp,"\nAge:%d",s.age);
  12. fprintf(fp,"\nGPA:%d",s.gpa);
  13.  
  14. }
  15. fclose(fp);
  16.  
  17.  
  18. getch();
  19.  
  20. }

Now in this part of the code we wrote a condition that if the file that we are trying to open is accessible or not. If the file is not accessible or the file cannot be opened then the program will give an error. Otherwise the else part will be executed.
In the else part we wrote a function called fprintf(). Function writes data into the file. The first argument that is given to this function is the file name so that it knows where to write the data. Then we place the format specifier just like in the normal printf() function. And then we wrote the name of the variable that we whose value we want to write in the file.
We wrote three fprintf() statements to write all the data that was palced in the structure student. After that we closed the file and ended the program.
Execute > compile
then Execute > run
Output

picture viwerpicture viwer


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.