Basic File I/O in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

The last chapter explained the standard input and output devices handled by C programming language. This chapter cover how C programmers can create, open, close text or binary files for their data storage.

A file represents a sequence of bytes, regardless of it being a text file or a binary file. C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices. This chapter will take you through the important calls for file management.

Opening Files

You can use the fopen( ) function to create a new file or to open an existing file. This call will initialize an object of the type FILE, which contains all the information necessary to control the stream. The prototype of this function call is as follows −

FILE *fopen( const char * filename, const char * mode );

Here, filename is a string literal, which you will use to name your file, and access mode can have one of the following values −
 

ModeDescription
rOpens an existing text file for reading purpose.
wOpens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file.
aOpens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content.
r+Opens a text file for both reading and writing.
w+Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.
a+Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.


If you are going to handle binary files, then you will use following access modes instead of the above mentioned ones −

"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"

Closing a File

To close a file, use the fclose( ) function. The prototype of this function is −

int fclose( FILE *fp );

The fclose(-) function returns zero on success, or EOF if there is an error in closing the file. This function actually flushes any data still pending in the buffer to the file, closes the file, and releases any memory used for the file. The EOF is a constant defined in the header file stdio.h.

There are various functions provided by C standard library to read and write a file, character by character, or in the form of a fixed length string.

Writing a File

Following is the simplest function to write individual characters to a stream −

int fputc( int c, FILE *fp );

The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error. You can use the following functions to write a null-terminated string to a stream −

int fputs( const char *s, FILE *fp );

The function fputs() writes the string s to the output stream referenced by fp. It returns a non-negative value on success, otherwise EOF is returned in case of any error. You can use int fprintf(FILE *fp,const char *format, ...) function as well to write a string into a file. Try the following example.

Make sure you have /tmp directory available. If it is not, then before proceeding, you must create this directory on your machine.

#include <stdio.h>

main() {
FILE *fp;

fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...
");
fputs("This is testing for fputs...
", fp);
fclose(fp);
}

When the above code is compiled and executed, it creates a new file test.txt in /tmp directory and writes two lines using two different functions. Let us read this file in the next section.

Reading a File

Given below is the simplest function to read a single character from a file −

int fgetc( FILE * fp );

The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error, it returns EOF. The following function allows to read a string from a stream −

char *fgets( char *buf, int n, FILE *fp );

The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string.

If this function encounters a newline character ' ' or the end of the file EOF before they have read the maximum number of characters, then it returns only the characters read up to that point including the new line character. You can also use int fscanf(FILE *fp, const char *format, ...) function to read strings from a file, but it stops reading after encountering the first space character.

#include <stdio.h>

main() {

FILE *fp;
char buff[255];

fp = fopen("/tmp/test.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s
", buff );

fgets(buff, 255, (FILE*)fp);
printf("2: %s
", buff );

fgets(buff, 255, (FILE*)fp);
printf("3: %s
", buff );
fclose(fp);

}

When the above code is compiled and executed, it reads the file created in the previous section and produces the following result −

1 : This
2: is testing for fprintf...

3: This is testing for fputs...

Let's see a little more in detail about what happened here. First, fscanf() read just This because after that, it encountered a space, second call is for fgets() which reads the remaining line till it encountered end of line. Finally, the last call fgets() reads the second line completely.

Binary I/O Functions

There are two functions, that can be used for binary input and output −

size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
              
size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);

Both of these functions should be used to read or write blocks of memories - usually arrays or structures.

The document Basic File I/O in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Basic File I/O in C Programming - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is file I/O in C programming?
Ans. File I/O (Input/Output) refers to the process of reading data from and writing data to files in a computer system. In C programming, file I/O is accomplished using file handling functions that allow us to create, read, write, update, and delete files.
2. How do I open a file for reading in C programming?
Ans. To open a file for reading in C programming, you can use the fopen() function. This function takes two arguments: the file name (along with the path if necessary) and the mode of operation. To open a file for reading, you need to use "r" as the mode. For example, the syntax for opening a file named "data.txt" for reading is: FILE *file = fopen("data.txt", "r");
3. How do I write data to a file in C programming?
Ans. To write data to a file in C programming, you can use the fprintf() function. This function takes three arguments: the file pointer (obtained by opening the file using fopen()), the format string (similar to printf()), and the data to be written. For example, to write an integer variable "num" to a file, you can use: fprintf(file, "%d", num);
4. How do I read data from a file in C programming?
Ans. To read data from a file in C programming, you can use the fscanf() function. This function takes three arguments: the file pointer, the format string (similar to scanf()), and the variables where the read data will be stored. For example, to read an integer from a file into an integer variable "num", you can use: fscanf(file, "%d", &num);
5. How do I close a file in C programming?
Ans. To close a file in C programming, you can use the fclose() function. This function takes a single argument, which is the file pointer of the file to be closed. For example, to close a file named "data.txt" that was opened using fopen(), you can use: fclose(file);
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Important questions

,

shortcuts and tricks

,

Viva Questions

,

Basic File I/O in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Free

,

mock tests for examination

,

Summary

,

study material

,

ppt

,

Sample Paper

,

video lectures

,

practice quizzes

,

Basic File I/O in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Extra Questions

,

past year papers

,

pdf

,

Exam

,

MCQs

,

Objective type Questions

,

Semester Notes

,

Previous Year Questions with Solutions

,

Basic File I/O in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

;