File Operations in C

Last updated on Jul 26,2023 22K Views

File Operations in C

edureka.co

File is a structure defined in ‘stdio.h’ to handle file operations. Various operations like opening the file reading/writing in a file and closing the file can be done. The above operations can be done as shown below:

To Open a File:

Read/ Write to a File:

The syntax for reading or writing a character is shown below:

Close a File  – int fclose(FILE* fp);

Example – Opening a File:

#include<stdio.h>
int main()
{
                FILE* fp;
                char ch;
                fp-fopen(“test.txt”, “r”);
                do
{
ch=getc(fp);
printf(“%c”,ch);
}
while(ch !=EOF);
fclose(fp);
printf(“
”);
system(“PAUSE”);
return 0;
}

Below is the file ‘test.txt’.

On compiling and running the program, the following output is obtained.

Program – When Trying To Open a File That Does Not Exist:

In case there is no file by name ‘test1’, the program returns the value NULL. In such cases, it needs to return the message “Unable to open the file.”

#include<stdio.h>
int main()
{
                FILE* fp;
                char ch;
                fp-fopen(“test1.txt”, “r”);
if (fp == NULL)
{
                printf(“Unable to open file
”);
                system(“PAUSE”);
                Return 0;
}
                do
{
ch=getc(fp);
printf(“%c”,ch);
}
while(ch !=EOF);
fclose(fp);
printf(“
”);
system(“PAUSE”);
return 0;
}

On compiling and running, the output as shown below:

Putting a Character in a File:

Example 1:

#include<stdio.h>
int main()
{
                FILE* fp;
                char ch;
                fp-fopen(“test1.txt”, “r”);
if (fp == NULL)
{
                printf(“Unable to open file
”);
                system(“PAUSE”);
                Return 0;
}
                do
{
ch=getc(fp);
putc(ch,stdout);
}
while(ch !=EOF);
fclose(fp);
printf(“
”);
system(“PAUSE”);
return 0;
}

The output is as shown below;

Example 2:

#include<stdio.h>
int main()
{
                FILE* fp1,*fp2;
                char ch;
                fp1=fopen(“test1.txt”, “r”);
if (fp1 == NULL)
{
                printf(“Unable to open file
”);
                system(“PAUSE”);
                Return 0;
}
fp2=fopen(“dest1.txt”, “w”);
if (fp2 == NULL)
{
printf(“Unable to open file
”);
                system(“PAUSE”);
                Return 0;
}
                while((ch =getc(fp1)) != EOF)
{
putc(ch,fp2);
}
fclose(fp1);
printf(“
”);
system(“PAUSE”);
return 0;
}

On running and compiling the program, a file with the name ‘dest1’ will be created with the content present in the file ‘test1’, in the folder.

Got a question for us? Please mention them in the comments section and we will get back to you.

Related Posts:

C& Data Structures Training

Bubble Sorting in C

Understanding Structures and Padding in C

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial