Category: File Operations in C
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: #include<stdio.h> #include<conio.h> void main() { char *str; FILE *fp; //Open the file in write mode fp = fopen(“write.txt”, “w”); printf(“Enter a string: “); gets(str); //write string in a file fputs(str, fp); getch(); } C Programming is brought you by LearnCOnline.com
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. #include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch; fp = fopen(“file1.C”,”r”); while(1) { ch = fgetc(fp); if (ch == EOF) break; printf(“%c”, ch); } getch(); fclose(fp); } C Programming is brought you by LearnCOnline.com
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: fclose(fp); 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...
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: ch = fgetc(fp); 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...
We can perform read and write operations on a file in C. But, before that we must open a file. To open a file in C, we have to call the function fopen() as shown below: FILE *fp; fp = fopen(“file1.C”,”r”); The above statement would open file named “file1.C” in “read” mode. It tells the computer that the file being opened would be used for reading purpose only. fopen() performs three important tasks when you open the file in “r” mode: Firstly, it searches the file to be opened on the disk Then it loads the file from the disk into...
As we all know, the operations we perform via. printf and scanf, reads and stores data in the memory. But this memory is limited for use. If in case we need to read and store large amount of data then this limited memory can’t be used. We need to find some ways to store this data permanently so that we can read and write to it at later point of time. This is where file operations comes into picture. C provides us with the facility to perform File operations in C programming language. File Operations in C Programming Language: There...