-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFile_Handling_System_Calls.c
More file actions
110 lines (89 loc) · 3.4 KB
/
Copy pathFile_Handling_System_Calls.c
File metadata and controls
110 lines (89 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* ============================================================================
* File Handling System Calls
* ============================================================================
*
* @file File_Handling_System_Calls.c
* @author Amey Thakur
* @repository https://github.com/Amey-Thakur/OPERATING-SYSTEM-AND-OPERATING-SYSTEM-LAB
* @experiment Experiment 4 - Operating System Lab
*
* @description This program demonstrates basic file handling system calls in C.
* It allows the user to:
* 1. create a file,
* 2. write data to the file, and
* 3. read data from the file.
*
* @functions fopen() - Opens a file
* fclose() - Closes a file
* fprintf()- Writes formatted output to a file
* fgetc() - Reads a character from a file
* scanf() - Reads input from standard input
* printf() - Writes output to standard output
*
* ============================================================================
*/
#include <stdio.h>
/**
* @brief Main function to demonstrate file operations
* @return 0 on successful execution
*/
int main()
{
/* File pointer to handle file operations */
FILE *fp;
/* Variables to store file name and character buffer */
char ch;
char filename[50];
char data[100];
printf("============================================\n");
printf(" File Handling System Calls Demo\n");
printf("============================================\n\n");
/* -------------------- 1. Open/Create File -------------------- */
printf("Enter the filename to create/open: ");
scanf("%s", filename);
/* Open file in write mode ("w")
If file doesn't exist, it is created. If it exists, content is cleared. */
fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error: Could not open file.\n");
return 1;
}
/* Close the file immediately (just creating it for now) */
fclose(fp);
printf("File '%s' created successfully.\n\n", filename);
/* -------------------- 2. Write to File -------------------- */
/* Re-open file in write mode to add content */
fp = fopen(filename, "w");
printf("Enter content to write to the file:\n");
/* Clear input buffer before reading string with spaces */
while ((getchar()) != '\n');
/* Read a line of text (including spaces) from user */
scanf("%[^\n]s", data);
/* Write user input to the file */
fprintf(fp, "%s", data);
/* Close the file to save changes */
fclose(fp);
printf("Data written to file successfully.\n\n", filename);
/* -------------------- 3. Read from File -------------------- */
printf("Reading content from '%s':\n", filename);
printf("--------------------------------------------\n");
/* Open file in read mode ("r") */
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error: Could not read file.\n");
return 1;
}
/* Read character by character until End Of File (EOF) */
ch = fgetc(fp);
while (ch != EOF)
{
printf("%c", ch);
ch = fgetc(fp);
}
/* Close the final file handler */
fclose(fp);
printf("\n--------------------------------------------\n");
printf("\nFile operation completed successfully.\n");
return 0;
}