Home C-tutorials C file open example



C file open example
Posted on: February 5, 2009 at 12:00 AM
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:

Related Tags for C file open example:
clibraryfileidefunctionsfunctionfunioheaderviusingoperaidopenstduncfopentostdiofopeildesliblipeinraroperationsheadoperationadestdfuncpenprorelsatratiolategoeaarrelatessthstatiprono


More Tutorials from this section

Ask Questions?    Discuss: C file open example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.