C file open example

Ads
 

C file open example

The header file provides several functions related to file operations. Here we are going to open a file using library function fopen().

The header file provides several functions related to file operations. Here we are going to open a file using library function fopen().

C file open example

     

This section demonstrates you how to open a file in C. 

The header file <stdio.h> provides several functions related to file operations. Here we are going to open a file using library function fopen(). You can see in the given example, we have used the type FILE in order to store the information related to file stream. Then we create a file pointer and call the function fopen("MYFILE.txt", "a") using append mode a. This will open the file MYFILE.txt and allow to perform write operations. After that the fprintf() function write the string to the file MYFILE.txt

Here i am using TurboC++ IDE and my source file is in the C:\TC\BIN directory and the output file MYFILE.txt will generate in the same bin directory.

Here is the code:

FILEOPEN.C

#include <stdio.h>
#include <conio.h>
void main() {
  FILE *fp;
  fp = fopen("MYFILE.txt""a");
  fprintf(fp, "%s\n ""Hello World, Where there is will, there is a way.");
  fclose(fp;
}

When you run the above example, you will find that the file MYFILE.txt  is created and contains string as 
Hello World
Where there is will, there is a way.

MYFILE.txt

Download Source Code:

Ads

Ads