File Handling – Let us C https://c.praveshagrawal.com Example C Programs Sat, 02 Dec 2023 11:08:34 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 Binary File Write and Read https://c.praveshagrawal.com/2023/12/02/binary-file-write-and-read/ https://c.praveshagrawal.com/2023/12/02/binary-file-write-and-read/#respond Sat, 02 Dec 2023 11:08:34 +0000 https://c.praveshagrawal.com/?p=74 #include <stdio.h> struct Student { char name[50]; int age; float gpa; }; int main() { // Binary file write and read FILE *binaryFile; struct Student student; // Writing to a binary file binaryFile = fopen("students.dat", "wb"); if (binaryFile != NULL) { strcpy(student.name, "John Doe"); student.age = 20; student.gpa = 3.75; fwrite(&student, sizeof(struct Student), 1, binaryFile); fclose(binaryFile); printf("Student data written to the binary file.\n"); } else { printf("Unable to open the binary file.\n"); } // Reading from a binary file binaryFile = fopen("students.dat", "rb"); if (binaryFile != NULL) { fread(&student, sizeof(struct Student), 1, binaryFile); fclose(binaryFile); printf("Student Data:\nName: %s\nAge: %d\nGPA: %.2f\n", student.name, student.age, student.gpa); } else { printf("Unable to open the binary file.\n"); } return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/binary-file-write-and-read/feed/ 0 File Read and Display https://c.praveshagrawal.com/2023/12/02/file-read-and-display/ https://c.praveshagrawal.com/2023/12/02/file-read-and-display/#respond Sat, 02 Dec 2023 11:08:11 +0000 https://c.praveshagrawal.com/?p=72 #include <stdio.h> int main() { // File read and display FILE *file; char buffer[100]; file = fopen("display.txt", "r"); if (file != NULL) { while (fgets(buffer, sizeof(buffer), file) != NULL) { printf("%s", buffer); } fclose(file); } else { printf("Unable to open the file.\n"); } return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/file-read-and-display/feed/ 0 File Append https://c.praveshagrawal.com/2023/12/02/file-append/ https://c.praveshagrawal.com/2023/12/02/file-append/#respond Sat, 02 Dec 2023 11:07:48 +0000 https://c.praveshagrawal.com/?p=70 #include <stdio.h> int main() { // File append FILE *file; char data[] = "This text will be appended.\n"; file = fopen("append.txt", "a"); if (file != NULL) { fprintf(file, "%s", data); fclose(file); printf("Data appended to the file.\n"); } else { printf("Unable to open the file.\n"); } return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/file-append/feed/ 0 File Copy https://c.praveshagrawal.com/2023/12/02/file-copy/ https://c.praveshagrawal.com/2023/12/02/file-copy/#respond Sat, 02 Dec 2023 11:07:25 +0000 https://c.praveshagrawal.com/?p=68 #include <stdio.h> int main() { // File copy FILE *sourceFile, *destinationFile; char buffer[100]; sourceFile = fopen("source.txt", "r"); destinationFile = fopen("destination.txt", "w"); if (sourceFile != NULL && destinationFile != NULL) { while (fgets(buffer, sizeof(buffer), sourceFile) != NULL) { fputs(buffer, destinationFile); } fclose(sourceFile); fclose(destinationFile); printf("File copied successfully.\n"); } else { printf("Unable to open files.\n"); } return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/file-copy/feed/ 0 File Input and Output https://c.praveshagrawal.com/2023/12/02/file-input-and-output/ https://c.praveshagrawal.com/2023/12/02/file-input-and-output/#respond Sat, 02 Dec 2023 11:07:05 +0000 https://c.praveshagrawal.com/?p=66 #include <stdio.h> int main() { // File input and output FILE *file; char data[] = "Hello, File Handling!\n"; // Writing to a file file = fopen("output.txt", "w"); if (file != NULL) { fprintf(file, "%s", data); fclose(file); printf("Data written to the file.\n"); } else { printf("Unable to open the file.\n"); } // Reading from a file file = fopen("output.txt", "r"); if (file != NULL) { char buffer[100]; fgets(buffer, sizeof(buffer), file); fclose(file); printf("Data read from the file: %s", buffer); } else { printf("Unable to open the file.\n"); } return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/file-input-and-output/feed/ 0