Java/J2EE and SOA (349 Blogs) Become a Certified Professional

File Operations in C

Last updated on Jul 26,2023 22.2K Views


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:

  • The syntax for opening the file is: File* fopen(char* name, char* mode);
  • Opens the file ‘name’ and returns the FILE pointer.
  • Values for ‘mode’ are ‘r’,’w’and ‘a’.
  • fopen returns NULL if  its unable to open file.
  • If file does not exist, fopen will create file, if mode is ‘w’ or ‘a’.

Read/ Write to a File:

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

  • Read a single character – int getc(FILE* fp);
  • Write a single character – int putc(intc, FILE* fp);

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:

File Operations in C

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;

File Operations in C

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 Course Online
Course NameDateDetails
Java Course Online

Class Starts on 12th October,2024

12th October

SAT&SUN (Weekend Batch)
View Details
Java Course Online

Class Starts on 14th December,2024

14th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

File Operations in C

edureka.co