File opening modes and file pointer c tutorial

Leave a Comment
This C programming tutorial explains following concepts
  1. Open a c file and what are the opening modes
  2. What is a File pointer
  3. Close a c file
  4. Read and write character to a file

How to open a file using c language? Also discuss its different file opening modes.
File Opening:
  • Before read or write operation, a file should be opened.
  • All standard file handing function of C are declared in stdio. h header file
  • When a file is opened a point of memory is reserved for file to read and write this memory is called memory buffer.
  • When a file is read it is first stored in the memory buffer and then program reads the file from the memory buffer.
  • When a file is written, then it is written in buffer, then its contents are transferred on the disk.
  • The transfer of data from buffer to the disk is called flushing of buffer.
  • The writing and reading through file through disk is time consuming, therefore time of reading and writing data to a file is reduced through buffer.

The File Pointer:
  • A pointer is like a variable whose content is the address of another memory cell (variable).
  • Represents a pointer to the variable with which it is used.
  • A file is not directly accessible in C.
  • File are accessed through I\O buffers.
  • A file pointer is a variable of FILE type defined in stdio.h
  • FILE* represents a pointer to a variable of FILE type.
  • The fopen( )function is used to open a file.
 Prototype is: FILE*fopen(const char*filename,const char*mode);
 FILE*fp; // it specifies the file pointer.

File opening Modes:

  • “r” open text file for reading,each file must exists before reading.
  • “w” open a text file for writing, if the file already exists then its contents are overwritten, if it does not exists, it will be created.
  • “a” open a text file for append. Data is appended at the end of existing file. If the file does not exist, it will be created.
  • “r+” open a file for both reading and writing. The file must already exists
  • “w+” open a file for reading and writing, its contents are overwritten if the file does not exist, it is created.
  • “a+” open a text file for both reading and appending. If the file does not exist, it is created for both reading and writing.

The fopen function returns the null pointer, if it fails to open the file (most common reason is file does not exist).

How to close a file in the c language?
File Closing:

  • An open must be close after completing the task.
  • When a file is close then the buffer is flushed (data is transferred on the disk).
  • If the file is not close than the data may be lost.
  • The fclose () function is used to close a file.
  • Syntax:
Where fp is a file pointer associated to a file which is to be closed.
fclose () function returns 0 if the close operation is successfully completed.
fclose () function returns EOF if any error occurs.

How to read and write character to a file using C language?

  • Once a file has been opened depending on its opening mode then either a character can be read from a file or character can be written to a file using the following function
  • Putc( ):
  • The putc( ) function writes a single character to the file.
  • The putc ( ) function returns the characters, if it successfully writes it.
  • The putc ( ) function returns the EOF if an error occurs.
putc(character ch , file_pointer);

see example: copy data from one text file to another c source code

Character ch can be character, int, constant or a variable.
File pointer specifies variable of FILE type associated with the file in witch character is to be written.

Stream types and file makers bof eof and newline c programming tutorial

Leave a Comment
This C programming tutorial covers two topics

  1. What is stream and its types in c programming
  2. How to use file makers EOF, BOF and newline 

What is a stream in  c programming? Also discuss its different types.
The data is stored in the form of file
A file is a set of related records.

  • It is a logical interface to a file.
  • It is associated to a file using an open operation.
  • It is not associated to a file using a close operation.
  • Stream refers to flow of data from source is know as reading ,fetching, getting or extracting the data.
  • The process of producing output data to the destination is know as writing, storing, inserting or putting the data.

There are two types of streams.

  1. Text stream
  2. Binary stream

Text stream:

  • A text stream is a sequence of characters.
  • Character translation may occur in a text stream e.g. new line is represented as carriage return.
  • There may not be one to relation between the characters written and those on the external device.

Binary Stream:

  • It is a sequence of bytes.
  • The number of bytes written or read is always the same as those on the external device.it means there is one to one relation between the bytes written or read and those on the external device.
  • No character translations occur in binary stream.
  • Some additional bytes such as the file sector on the disk are added to to the binary stream.

What are file markers? Explain the use of BOF, EOF and New Line in c programming.

  • A file contains a numbers of characters or bytes each file stored on a disk has a start and an end.
  • The start of first byte or character is called beginning of file(BOF).
  • The end of last byte or character is called End of file (EOF).
  • The position where data read or data write operations are performed is called current position.
  • EOF character are placed after the last character i C.
  • Enter key is used in text editor for new line but in C new line character is \n to be placed at the of each line.

How strings are stored and retrieved in C language

Leave a Comment
String Handling in C programming with common functions
  • String is a combination of characters and it is enclosed in double quotes
  • Character array is used to hold string.
  • Char name_ of_string[ string _ length];
  • Char is data type and string length is the name of characters in a string.
  • A string variable can also be declared without specifying the number of characters.
  • The variables that is used to store a string is called string variables.
  • The last character of a string must be a Null character \0 i.e. Null character \0 is added the end of the string.
  • Null character \0 is also included in the string length.

The string can be initialized
char languageName[20]="cprogramming";
char languageName[ ]   ="cprogramming";

The most common string function are:
  • fputs( )
  • fgets( )
  • strcpy( )
What is fputs() function? Explain with the help of an example.
  • It is used to write a string of characters in to a file
  • The function writes the string until the null character \0 is reached
  • It does not add the null character of the file
  • The file must be opened or in append mode to write this funcation

fputs() Example
fputs(string, file_pointer);


Fp = fopen(“test1.txt”,”w”);

fputs(“The string of your choice:”,fp);

The above string is written in test 1.txt file.

What is fgets() funcation? Explain with the help of an example.

  • It is used to read a string of characters of a certain length from a file.
  • The file must be opened or in append mode to write this function.

fgets(string or string variable, n, file _pointer)
N is the number of characters read from the file.


copy data from one text file to another c source code

Write a c/c++ program which reads one text file and writes its data to another file. Hint use functions gets()  and fputc().

Concept used in this tutorial

Program explanation:
  • We have two files Input(text.txt) and output(text2.txt) data will be read from input file and write to output file which means that input file will be opened in read mode "r" and output file will be opened in write mode "r".
  • We have two if conditions which ensures files are accessible or not.
  • Using getc() function in while loop 'ch' is assigned character by character of input file then write into the output file using fuptc() function until it reaches EOF(end of file)
Source code

#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

int main()
    FILE *input;
    FILE *output;

    char ch;

    input = fopen("text.txt","r");
    if(input == NULL){

       printf("\n Required File text.txt failed to open ");

    } else {
       output = fopen("text2.txt","w");
           if(input == NULL){

            printf("\n Required File text2.txt failed to open ");

          } else { //File opened successfully

                while((ch = getc(input))!= EOF) {
                      fputc(ch, output);

             printf("\n Data is Copied ");



    fclose(input );

    return 0;

To read and write file in your desire director give path in fopen function e.g
It is recommended to do experiment with code and examine the output of program.