Unit 8 File Management - Computer Programming PDF

Summary

This document provides notes on file management in C programming. It details various file operations including creation, opening, reading, writing, and closing files. It also includes examples and explanations of different file modes.

Full Transcript

Department of CE 01CE0101 - Computer Unit – 8 File Management Programming  A File can be used to store a large volume of persistent data. Like many other languages 'C' provides following file management functions, Fil...

Department of CE 01CE0101 - Computer Unit – 8 File Management Programming  A File can be used to store a large volume of persistent data. Like many other languages 'C' provides following file management functions, File  Creation of a file Managem  Opening a file ent  Reading a file  Writing to a file  Closing a file Important file managem ent functions  Whenever we want to work with a file, the first step is to create a file. A file is nothing but space in a memory where data is stored.  To create a file in a ‘C’ program following syntax is used, FILE *fp; fp = fopen ("file_name", "mode"); Create a  In the above syntax, the file is a data structure which is defined in the standard library. File fopen is a standard function which is used to open a file. If the file is not present on the system, then it is created and then opened. If a file is already present on the system, then it is directly opened using this function. fp is a file pointer which points to the type file.  Whenever you open or create a file, you have to specify what you are going to do with the file.  A file in ‘C’ programming can be created or opened for reading/writing purposes. File modes  A mode is used to specify whether you want to open a file for any of the below- given purposes.  Following are the different types of modes in ‘C’ programming which can be used while working with a file. File Description Mode Open a file for reading. If a file is in reading mode, r then no data is deleted if a file is already present on a system. Open a file for writing. If a file is in writing mode, then a new file is created if a file doesn’t exist at all. w If a file is already present on a system, then all the data inside the file is truncated, and it is opened for File modes writing purposes. Open a file in append mode. If a file is in append mode, then the a file is opened. The content within the file doesn’t change. r+ open for reading and writing from beginning w+ open for reading and writing, overwriting a file a+ open for reading and writing, appending to file #include int main() { FILE *fp; Example fp = fopen ("data.txt", "w"); } You can specify the path where you want to create your file. #include Example int main() { FILE *fp; fp = fopen ("D://data.txt", "w"); }  One should always close a file whenever the operations on file are over.  It means the contents and links to the file are terminated. This prevents Close a file accidental damage to the file.  ‘C’ provides the fclose function to perform file closing operation.  The syntax of fclose is as follows, fclose (file_pointer); FILE *fp; fp = fopen ("data.txt", "r"); fclose (fp);  The fclose function takes a file pointer as an argument. The is then closed with the help of fclose function file associated with Example the file pointer  It returns 0 if close was successful and EOF (end of file) if there is an error has occurred while file closing.  After closing the file, the same file pointer can also be used with other files.  In ‘C’ programming, files are automatically close when the program is terminated. Closing a file manually by writing fclose function is a good programming practice.  In C, when you write to a file, newline characters ‘\n’ must be explicitly added.  The stdio library offers the necessary functions to write to a file:  fputc(char, file_pointer): It writes a Writing to character to the file pointed to by a File file_pointer.  fputs(str, file_pointer): It writes a string to the file pointed to by file_pointer.  fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to by file_pointer. The string can optionally include format specifiers and a list of variables variable_lists. #include } int main() fclose(fp); { return 0; FILE *fp; } int i;  The above program char str[] = "File writes a single character fputc() operations, writing the into the data.txt file until it reaches the next Function data here...\n"; line symbol “\n” which fp = fopen ("data.txt", indicates that the "w"); sentence was for (i = 0; str[i] != '\ successfully written. n'; i++)  The process is to take { each character of the array and write it into fputc(str[i], fp); the file. fputc()  In the above program, we have created and Function opened a file called fputc_test.txt in a write mode and declare our string which will be written into the file.  We do a character by character write operation using for loop and put each character in our file until the “\n” character is encountered then the file is closed using the fclose function. #include fclose(fp); int main() return (0); { } FILE * fp;  In the above program, we have created and fp = fopen(“fputs.txt", fputs () "w+"); opened a file called fputs.txt in a write Function fputs(“1.This is fputs mode.  After we do a write function,", fp); operation using fputs() fputs("We don't need function by writing two different strings to use for loop\n", fp);  Then the file is closed using the fclose function. #include int main() {  In the above FILE *fptr; program we have fptr = created and opened fopen("fprintf_test.txt", a file called fprintf() "w"); fprintf_test.txt in a Function write mode. fprintf(fptr, "Learning  After a write C Programming \n"); operation is fclose(fptr); performed using fprintf() function by return 0; writing a string, then } the file is closed using the fclose function.  There are three different functions dedicated to reading data from a file  fgetc(file_pointer): It returns the next character from the file pointed to by the file pointer. When the end of the file has been reached, the EOF is sent back. Reading  fgets(buffer, n, file_pointer): It reads n-1 data from characters from the file and stores the string in a buffer in which the NULL character ‘\0’ is a File appended as the last character.  fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and analyze data. It reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. Keep in mind that as with scanf, fscanf stops reading a string when space or newline is encountered. #include str1); int main() printf("Read String2 |%s|\n", str2); { printf("Read String3 |%s|\n", FILE * fp; str3); char b, c; printf("Read String4 |%s|\n", Reading fp= fopen("fputs.txt", "r"); str4); data from printf("----read a line----\n"); fgets(b, 50, fp); printf("----read the entire a File printf("%s\n", b); file----\n"); fp = fopen("fputs.txt", "r"); using printf("----read and parse data----\n"); while ((c = getc(fp)) != EOF) fgets fp = fopen("fputs.txt", "r"); printf("%c", c); char str1, str2, fclose(fp); str3, str4; return 0; fscanf(fp, "%s %s %s %s", str1, } str2, str3, str4); printf("Read String1 |%s|\n", Reading data from a File using fgets 1. In the above program, we have opened the file called “fputs.txt” which was previously written using fprintf() function, and it contains “Welcome to Marwadi university” string. We read it using the Reading fgets() function which reads line by line data from where the buffer size must be enough to handle the entire line. a File 2. We reopen the file to reset the pointer file using to point at the beginning of the file. fgets Create various strings variables to handle each word separately. Print the variables to see their contents. The fscanf() is mainly used to extract and parse data from a file. 1. Reopen the file to reset the pointer file to point at the beginning of the file. Read data and print it from the Reading file character by character using data from getc() function until the EOF a File statement is encountered using 2. After performing a reading operation file using different variants, we fgets again closed the file using the fclose function. #include #include Program to int main() { copy source char ch;// source_file, target_file; file to FILE *source, *target; destination char source_file[]="x1.txt"; char target_file[]="x2.txt"; file. source = fopen(source_file, "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(target_file, "w"); Hi welcome to Marwadi if (target == NULL) { X2.txt fclose(source); After execution Program to printf("Press any key to exit...\n"); X1.txt exit(EXIT_FAILURE); Hi welcome to Marwadi copy source } X2.txt file to while ((ch = fgetc(source)) != EOF) fputc(ch, target); Hi welcome to Marwadi Note: After execution content of X1.txt destination printf("File copied successfully.\n"); copied and pasted into X2.txt file. fclose(source); fclose(target); return 0; } Output: Before execution X1.txt Thank You!

Use Quizgecko on...
Browser
Browser