Published on Oct 09,2014
742 Views
Email Post

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(“\n”);
system(“PAUSE”);
return 0;
}

Below is the file ‘test.txt’.

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

File Operations in C

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\n”);
                system(“PAUSE”);
                Return 0;
}
                do
{
ch=getc(fp);
printf(“%c”,ch);
}
while(ch !=EOF);
fclose(fp);
printf(“\n”);
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\n”);
                system(“PAUSE”);
                Return 0;
}
                do
{
ch=getc(fp);
putc(ch,stdout);
}
while(ch !=EOF);
fclose(fp);
printf(“\n”);
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\n”);
                system(“PAUSE”);
                Return 0;
}
fp2=fopen(“dest1.txt”, “w”);
if (fp2 == NULL)
{
printf(“Unable to open file\n”);
                system(“PAUSE”);
                Return 0;
}
                while((ch =getc(fp1)) != EOF)
{
putc(ch,fp2);
}
fclose(fp1);
printf(“\n”);
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

String Function in C Programming

About Author
edureka
Published on Oct 09,2014

Share on

Browse Categories

Comments
0 Comments