lab17.
md 2024-11-10
CSE115L Week:09(File) Lab: 17
Name:
ID:
Section:
Example 01: C Program to read from console & write data into file.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 1000
int main()
{
char str[MAX_SIZE];
// creating a pointer to the File to work with files
FILE *fptr;
// opening file in writing mode
fptr = fopen("example.txt", "W"); // File Path & mode
// if file dose not exist or file creation fails
if (fptr == NULL)
{
printf("Unable to open file.\n");
printf("Please check whether file exists and you have write
privilege.\n");
exit(0);
}
printf("Enter a str:");
fgets(str, MAX_SIZE, stdin);
fprintf(fptr, "%s", str);
/* Done with this file, close file to release resource */
fclose(fptr);
return 0;
}
Example Input:
Enter a sentence : Always do right. This will gratify some people and
astonish the rest.
1/4
lab17.md 2024-11-10
Example 02 : C program to read a file and display file contents.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1000
int main()
{
/* File pointer to hold reference to our file */
FILE *fPtr;
char str[MAX_SIZE];
int len = 0;
/*
* Open file in r (read) mode.
* "data/example.txt" is complete file path to read
*/
fPtr = fopen("data/exaple.txt", "r");
/* fopen() return NULL if last operation was unsuccessful */
if (fPtr == NULL)
{
/* Unable to open file hence exit */
printf("Unable to open file.\n");
printf("Please check whether file exists and you have read
privilege.\n");
exit(0);
}
/* File open success message */
printf("File opened successfully. Reading file contents line by line.
\n\n");
/* Repeat this until read line is not NULL */
while (fgets(str, MAX_SIZE, fPtr) != NULL)
{
/* String Length */
len = strlen(str);
printf("%s\n", str);
}
/* Done with this file, close file to release resource */
fclose(fPtr);
return 0;
}
2/4
lab17.md 2024-11-10
Example 03: C program to take N student informtion as input from the console and saves it into a
text file
#include <stdio.h>
#define SIZE 100 // Adjust this value as needed
struct Student
{
char fname[SIZE];
char lname[SIZE];
int id;
float cgpa;
};
int main()
{
int num_students;
printf("Enter the number of students: ");
scanf("%d", &num_students);
struct Student students[num_students];
for (int i = 0; i < num_students; i++)
{
printf("\nEnter details for student %d:\n", i + 1);
printf("First name: ");
scanf("%s", students[i].fname); // Read entire line for name with
spaces
printf("Last name: ");
scanf("%s", students[i].lname);
printf("ID: ");
scanf("%d", &students[i].id);
printf("CGPA: ");
scanf("%f", &students[i].cgpa);
}
char filename[SIZE];
printf("\nEnter the filename to save the data: ");
scanf("%s", filename);
FILE *fp = fopen(filename, "w");
if (fp == NULL)
{
printf("Error opening file!\n");
exit(0);
}
for (int i = 0; i < num_students; i++)
{
fprintf(fp, "%s %s %d %.2f\n", students[i].fname,
students[i].lname, students[i].id, students[i].cgpa);
3/4
lab17.md 2024-11-10
}
fclose(fp);
printf("Student data saved to %s successfully!\n", filename);
return 0;
}
Example 04 : C program that reads the saved student data from the previous example and
displays it on the console.
#include <stdio.h>
#define SIZE 100 // Adjust this value as needed
struct Student
{
char fname[SIZE];
char lname[SIZE];
int id;
float cgpa;
};
int main()
{
char filename[SIZE];
printf("Enter the filename containing student data: ");
scanf(" %s", filename);
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error opening file!\n");
return 1;
}
struct Student student;
printf("\nStudent Data:\n");
// Read until end of file (EOF) is reached
while (fscanf(fp, "%s %s %d %f", student.fname, student.lname,
&student.id, &student.cgpa) != EOF)
{
printf("%s %s (%d) - CGPA: %.2f\n", student.fname, student.lname,
student.id, student.cgpa);
}
fclose(fp);
printf("Student data displayed successfully!\n");
return 0;
}
4/4