











Program to Write a File in C
How to Write file in C programming?
File is a very important feature in C programming as it helps to cope with stdout console drawbacks. These drawbacks may affect the outcome of the desired program. Here we would learn Writing File in C and its various ways.


Write Function in C Programming:
Writing function is used to insert data into a file that is opened by the fopen function. The file needs to be open in “w” mode as to insert data into the file. If the file name exists then the data is inserted into it or else a new file is made with the same name and data is inserted by varios predefined functions.
Syntax 1:
Using fprintf() function.
fprintf(fptr, "%s", sentence);
- fprintf(): this function is used to print the data or content in the file specified rather than the stdout console.
- fptr: It is the file pointer where the data is to be written into.
- sentence: it is the data that is to be written into the file pointed by the fptr.
Steps in writing file in C:
- Step 1: Start
- Step 2: Declare the file pointer.
- Step 3: Open the file in write mode using the file pointer.
- Step 4: Check if the file is empty or not.
- Step 5: Take the data that needs to be written into the file, either pre-define it or take from the user.
- Step 6: Insert data into the file
- Step 7: Display the data inserted into the file.
- Step 8: Close the file.
- Step 9: Program end.
Problem 1:
Write a program to write a file using fprintf where data is predefined.
Code:
#include<stdio.h> int main() { char *newfile = "test.txt"; // open the file for writing FILE *fp = fopen(newfile, "w"); if (fp == NULL) { printf("Error opening the file %s", newfile); return -1; } // write to the text file for (int i = 0; i < 10; i++) { fprintf(fp, "This is the line #%d\n", i + 1); } // close the file fclose(fp); return 0; }
Problem 2:
Write a program to write a file using fprintf where data is taken from the user.
Code:
#include<stdio.h> #include<stdlib.h> int main() { char s[1000]; // creating file pointer to work with files FILE *fptr; // opening file in writing mode fptr = fopen("text.txt", "w"); // exiting program if (fptr == NULL) { printf("Error!"); exit(1); } printf("Enter a sentence:\n"); fgets(s, sizeof(s), stdin); fprintf(fptr, "%s", s); fclose(fptr); return 0; }
Syntax 2:
Using fputc() function.
int putc(int char, FILE *stream)
- char: it is that character that is to be written into the file.
- stream: It is the file in which the character is going to be written into.
Problem 3:
Write a program to write a file using fputc where data is pre-defined.
Code:
#include<stdio.h> int main() { char *newfile = "text.txt"; // open the file for writing FILE *fptr = fopen(newfile, "w"); if (fptr == NULL) { printf("Error opening the file %s", newfile); return -1; } // write to the text file for (char ch = 'a'; ch < 'z'; ch++) fputc(ch, fptr); // close the file fclose(fptr); return 0; }
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment