File Handling in C Language
File Handling in C Language
1. Create
2. Write
3. Read
4. Delete
5. Size
/**
* C program to create a file and write data into file.
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
/* Variable to store user content */
char data[DATA_SIZE];
/*
* Open file in w (write) mode.
* "data/file1.txt" is complete path to create file
*/
fPtr = fopen("data/file1.txt", "w");
/* Success message */
printf("File created and saved successfully. :) \n");
return 0;
}
Output
Enter contents to store in file :
Hurray!!! I learned to create file in C programming. I also learned to
write contents to file. Next, I will learn to read contents from file on
Codeforwin. Happy coding ;)
File created and saved successfully. :)
#include<stdio.h>
int main() {
int del = remove("textFile.txt");
if (!del)
printf("The file is Deleted successfully");
else
printf("the file is not Deleted");
return 0;
}
#include<stdio.h>
int main()
{
if (remove("abc.txt") == 0)
printf("Deleted successfully");
else
return 0;
READ A FILE IN C
#include <stdio.h>
int main()
{
char name[50];
int marks, i, num;
FILE *fptr;
fptr = (fopen("C:\\student.txt", "w"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fclose(fptr);
return 0;
}
FILE SIZE IN C
#include<stdio.h>
#include<conio.h>
void main()
FILE *fp;
char ch;
int size = 0;
fp = fopen("MyFile.txt", "r");
if (fp == NULL)
else
{
printf("\nFile opened...");
fclose(fp);