Category: File Operations in C

Interested in learning C++ Programming? Click here...


c programming write text 2

Program to write a text to a file

We will write a program in C that will accept a string from the user and write the string in a file. In this program, we will use fopen function in order to open a file. We will also use fputs function which will write a string to a file. Below is the program:

C Programming is brought you by LearnCOnline.com

C programming read text from a file and display it on screen 1

Program to read text from a file and display it on screen

We will write a program in C that will read a text file character by character and displays it on the screen. In order to achieve it, we will use fopen function that will open the file from the disk. We will also use fgetc function which will read the file character by character and then display the content of the string character by character.

C Programming is brought you by LearnCOnline.com


Closing the File in C 2

Closing the File in C

Once we open a file, finished reading from the file, we need to close the file. This is done using the function fclose() through the statement:

Once we are done with closing the file, we can no longer read, write or perform any operation on the file unless we reopen it.  Note that to close the file, we don’t use the filename but the file pointer fp. On closing the file, the buffer associated with the file is removed from memory. While performing a write operation on a file, when we try to close the file using fclose(), three...

Reading from a File in C 2

Reading from a File in C

Once the file has been opened for reading using fopen(), as we have seen, the file’s contents are brought into buffer and a pointer is set up that points to the first character in the buffer. This pointer is one of the elements of the structure to which fp is pointing. To read the file’s contents from the memory, there exists a function called fgetc(). This has been used in our program as:

fgetc() performs the following operations: Reads the characters from the current pointer position Advances the pointer position so that it now points to the next character Returns the...