0% found this document useful (0 votes)
14 views

File Handling

This document discusses file handling in C programming. It covers opening, closing, reading from and writing to files. Functions covered include fopen(), fclose(), fprintf(), fscanf(), fgetc(), fputc(), fseek(), and feof(). Examples are provided to demonstrate how to create and write data to a file, read from an existing file, append additional data to a file, reposition the file pointer, and close files. Proper file handling is important for any program that needs to manipulate external data sources.

Uploaded by

sarthakmalik5098
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

File Handling

This document discusses file handling in C programming. It covers opening, closing, reading from and writing to files. Functions covered include fopen(), fclose(), fprintf(), fscanf(), fgetc(), fputc(), fseek(), and feof(). Examples are provided to demonstrate how to create and write data to a file, read from an existing file, append additional data to a file, reposition the file pointer, and close files. Proper file handling is important for any program that needs to manipulate external data sources.

Uploaded by

sarthakmalik5098
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Ankur Rastogi Unit-VI File Handling

Part of any useful computer program is often going to be manipulation of


external data sources. The file is the basic unit of storage for many operating
systems, from Unix to Mac. Any C development environment on these
platforms will include functions allowing the programmer to:

 Open & Close files;


 Read from & Write to files;
 Delete files.

File Handling Commands

Creating a file and output some data


In order to create files we have to learn about File I/O i.e. how to write data
into a file and how to read data from a file.

#include <stdio.h>

main( )

FILE *fp;

char stuff[25];

int index;

fp = fopen("ABC.TXT","w"); /* open for writing */

strcpy(stuff,"This is an example line.");

for (index = 1; index <= 10; index++)

fprintf(fp,"%s Line number %d\n", stuff, index);

fclose(fp); /* close the file before ending program */

getch();

2013 Page 1
Ankur Rastogi Unit-VI File Handling

Reading (r)
When an r is used, the file is opened for reading, a w is used to indicate a file to
be used for writing, and an indicates that you desire to append additional data to
the data already in an existing file. Most C compilers have other file attributes
available.

#include <stdio.h>

void main()

FILE *fp;

int c;

fp = fopen("ABC.TXT","r");

c = getc(fp) ;

while (c!= EOF)

putchar(c);

c = getc(fp);

fclose(fp);

getch();

2013 Page 2
Ankur Rastogi Unit-VI File Handling

Writing (w)
When a file is opened for writing, it will be created if it does not already exist
and it will be reset if it does, resulting in the deletion of any data already there.
Using the w indicates that the file is assumed to be a text file.

#include <stdio.h>

int main()

FILE *fp;

fp = fopen("file.txt","w");

/*Create a file and add text*/

fprintf(fp,"%s","This is just an example :)"); /*writes data to the file*/

fclose(fp); /*done!*/

return 0;

Appending (a)
When a file is opened for appending, it will be created if it does not already
exist and it will be initially empty. If it does exist, the data input point will be
positioned at the end of the present data so that any new data will be added to
any data that already exists in the file.

#include <stdio.h>

int main()

FILE *fp;

2013 Page 3
Ankur Rastogi Unit-VI File Handling

fp = fopen("file.txt","a");

fprintf(fp,"%s","This is just an example :)"); /*append some text*/

fclose(fp);

return 0;

Outputting to the file


The job of actually outputting to the file is nearly identical to the outputting we
have already done to the standard output device. The only real differences are
the new function names and the addition of the file pointer as one of the
function arguments. In the example program, fprintf replaces our familiar printf
function name, and the file pointer defined earlier is the first argument within
the parentheses. The remainder of the statement looks like, and in fact is
identical to, the printf statement.

EOF, getc() and feof()


In C, getc() returns EOF when end of file is reached. getc() also returns EOF
when it fails. So, only comparing the value returned by getc() with EOF is not
sufficient to check for actual end of file. To solve this problem, C provides
feof() which returns non-zero value only if end of file has reached, otherwise it
returns 0.

int main ()

FILE *fp;

int c;

int n = 0;

fp = fopen("file.txt","r");

if(fp == NULL)

2013 Page 4
Ankur Rastogi Unit-VI File Handling

printf("Error in opening file");

return(-1);

while(!feof(fp))

c = fgetc(fp);

printf("%c", c);

fclose(fp);

getch();

fseek()
When doing reads and writes to a file, the OS keeps track of where you are in
the file using a counter generically known as the file pointer. You can reposition
the file pointer to a different point in the file using the fseek() call. Think of it as
a way to randomly access you file.

Declaration
Following is the declaration for fseek() function.

int fseek(FILE *stream, long int offset, int whence)

Parameters
stream -- This is the pointer to a FILE object that identifies the stream.

offset -- This is the number of bytes to offset from whence.

whence -- This is the position from where offset is added. It is specified by one
of the following constants:

2013 Page 5
Ankur Rastogi Unit-VI File Handling

int main ()

FILE *fp;

fp = fopen("file.txt","w+");

fputs("This is tutorialspoint.com", fp);

fseek( fp, 7, SEEK_SET );

fputs(" C Programming Langauge", fp);

fclose(fp);

getch();

fputc()
#include<stdio.h>

#include<conio.h>

void main()

FILE *fp;

char ch;

fp=fopen("Ab.txt","w");

for(ch='A';ch<='z';ch++)

fputc(ch,fp);

2013 Page 6
Ankur Rastogi Unit-VI File Handling

putchar(ch);

fclose(fp);

getch();

fscanf()
#include<stdio.h>

void main()

FILE *fp;

char name[30],str[20];

fp=fopen("A.txt","r");

fscanf(fp, "%s %s",name,str);

printf("%s %s",name,str);

fclose(fp);

getch();

Closing a file
To close a file you simply use the function fclose with the file pointer in the
parentheses. Actually, in this simple program, it is not necessary to close the file
because the system will close all open files before returning to DOS, but it is
good programming practice for you to close all files in spite of the fact that they
will be closed automatically, because that would act as a reminder to you of
what files are open at the end of each program.

2013 Page 7
Ankur Rastogi Unit-VI File Handling

You can open a file for writing, close it, and reopen it for reading, then close it,
and open it again for appending, etc. Each time you open it, you could use the
same file pointer, or you could use a different one. The file pointer is simply a
tool that you use to point to a file and you decide what file it will point to.

#include <stdio.h>

main( )

FILE *fp;

char c;

fp = fopen("ABC.TXT", "r");

if (fp == NULL){

printf("File doesn't exist\n");

getch();

else {

do {

c = getc(fp); /* get one character from the file*/

putchar(c); /* display it on the monitor */

} while (c != EOF); /* repeat until EOF (end of file) */

fclose(fp);

getch();

2013 Page 8

You might also like