Ralph Roel M.
Bautista
Emil A. Corpuz BSCS 1C Comprog2
Write a c program that creates a text file named student.txt
Source Code:
#include <stdio.h>
int main() {
FILE *file; // Declare a pointer to a FILE structure to handle file operations
char filename[50]; // Declare an array of characters to store the filename entered
by the user
printf("Enter filename: "); // Prompt the user to enter a filename
scanf("%s", filename); // Read the filename entered by the user and store it in
the filename array
// Open the file with the specified filename in "write" mode ("w")
file = fopen(filename, "w");
// Check if fopen() was successful in opening the file
if (file == NULL) {
printf("Error creating file!\n"); // Print an error message if fopen() failed
return 1; // Return 1 to indicate an error occurred
printf("%s is successfully created.\n", filename); // Print a success message
indicating the filename
fclose(file); // Close the file
return 0; // Return 0 to indicate successful execution of the program
ScreenShot of Output:
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
Write a c program that print the contents of student.text file using feof(), fgets() and printf()
functions.
Source Code:
#include <stdio.h>
int main() {
FILE *file; // Declare a pointer to a FILE structure to handle file operations
char filename[] = "student.txt"; // Define the filename for the student data file
char student_id[50], full_name[100], course[50], age[10], address[100]; // Declare arrays to
store student data
int i; // Declare a loop counter variable
// Open the file for writing
file = fopen(filename, "w");
// Check if fopen() was successful in opening the file
if (file == NULL) {
printf("Error opening file!\n"); // Print an error message if fopen() failed
return 1; // Return 1 to indicate an error occurred
printf("*****Display Student Data*****\n");
// Loop to input student data
for (i = 0; i < 2; i++) {
printf("Enter Student ID: ");
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
fgets(student_id, sizeof(student_id), stdin); // Read student ID from the user
fprintf(file, "Student ID: %s", student_id); // Write student ID to the file
printf("Enter Full Name: ");
fgets(full_name, sizeof(full_name), stdin); // Read full name from the user
fprintf(file, "Full Name: %s", full_name); // Write full name to the file
printf("Enter Course: ");
fgets(course, sizeof(course), stdin); // Read course from the user
fprintf(file, "Course: %s", course); // Write course to the file
printf("Enter Age: ");
fgets(age, sizeof(age), stdin); // Read age from the user
fprintf(file, "Age: %s", age); // Write age to the file
printf("Enter Address: ");
fgets(address, sizeof(address), stdin); // Read address from the user
fprintf(file, "Address: %s", address); // Write address to the file
fclose(file); // Close the file
// Open the file for reading
file = fopen(filename, "r");
// Check if fopen() was successful in opening the file
if (file == NULL) {
printf("Error opening file!\n"); // Print an error message if fopen() failed
return 1; // Return 1 to indicate an error occurred
printf("\nStudent Data from File:\n");
// Read and display student data from the file
while (!feof(file)) {
char line[100];
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
if (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line); // Print each line of student data
fclose(file); // Close the file
return 0; // Return 0 to indicate successful execution of the program
ScreenShot of Output:
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2
Ralph Roel M. Bautista
Emil A. Corpuz BSCS 1C Comprog2