Ch5 File Handling 1
Ch5 File Handling 1
Structured Design-2
303105151
Chapter- 5
FILE HANDLING
CONTENT
● FILE Pointer
● File modes
● Opening a file
● Writing into a file
● Reading from a file
● Closing a file
● Some more function
File Pointer
● Definition: A FILE * variable is used to represent a
connection to a file. Think of it as a handle to the file.
● Syntax:
● FILE * variableName;
● variableName shall be used to store a file in itself
File Modes
● Basic FILE MODES :
● r - Read mode, we can open a file in “r” mode if we wish only to
read data from the file
● w - Write mode, we can open a file in “w” mode if we wish only
to write data into the file
● a - Append mode, we can open a file in “a” mode if we wish only
to append data into the file
OPENING A FILE
fopen(), is used open the file, if there is no file with
the specified name, NULL will be returned to FILE *.
Syntax:
FILE * fptr;
fptr = fopen(filename.txt, “mode”);
OPENING A FILE
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char filename = "myFile.txt";
fp = fopen(filename, "r");
if (fp == NULL) {
perror("Error opening file");
exit(1);
} else {
printf("File '%s' opened successfully.\n", filename);
}
fclose(fp);
return 0;
}
WRITING INTO A FILE
#include <stdio.h> fputs(): Writes the string str (excluding the
#include <stdlib.h> null terminator) to the output stream stream.
int main() { It returns a non-negative value on success,
FILE *fptr; // File pointer
fptr = fopen("myfile.txt", "w");
or EOF on error. It does not automatically
if (fptr == NULL) { add a newline.
printf("Error opening file!\n");
}
fprintf(fptr, "This is some text written to the file.\n");
fprintf(fptr, "Another line of text.\n");
fputs("A string written using fputs.\n", fptr);
fclose(fptr);
printf("File written to successfully!\n");
return 0;
}
WRITING INTO A FILE (alternate way using fgets())
#include <stdio.h>
#include <stdlib.h> fgets(): Reads at most n-1 characters
int main() { from the input stream stream into the
FILE *fptr;
char line[100];
string str, stopping at a newline or EOF.
fptr = fopen("myfile.txt", "r"); It null-terminates the string. Returns str
if (fptr == NULL) { on success, or NULL on error or EOF.
printf("Error opening file!\n");
exit(1);
}
printf("Contents of the file (line by line):\n");
while (fgets(line, sizeof(line), fptr) != NULL) {
printf("%s", line);
}
fclose(fptr);
return 0;
}
READING FROM A FILE
#include <stdio.h>
#include <stdlib.h> fgetc(): Reads the next character from
int main() {
FILE *fptr;
the input stream stream and returns its
char ch; integer value. Returns EOF (usually -1) on
fptr = fopen("myfile.txt", "r"); end-of-file or error.
if (fptr == NULL) {
printf("Error opening file!\n");
exit(1);
}
printf("Contents of the file:\n");
while ((ch = fgetc(fptr)) != EOF) {
printf("%c", ch); // Print each character to the console
}
fclose(fptr);
return 0;
}
CLOSING A FILE
#include <stdio.h>
#include <stdlib.h> fclose(): Closes the file associated
int main() { with the stream stream, flushing any
FILE *fptr;
fptr = fopen("myfile.txt", "w");
buffered output. Returns 0 on success,
if (fptr == NULL) { or EOF on error.
perror("Error opening file");
exit(1);
}
fprintf(fptr, "This is some text.\n");
if (fclose(fptr) == EOF) {
perror("Error closing file");
exit(1);
}
printf("File closed successfully.\n");
fptr = NULL;
return 0;
}
CLOSING A FILE
rewind(): Sets the file pointer to the beginning of the
fwrite(): Writes a block of data.
file.
fwrite(buffer, sizeof(int), 10, fptr); // Writes 10 ints from
rewind(fptr);
buffer
fread(): Reads a block of data.
ftell(): Returns the current file pointer position.
fread(buffer, sizeof(int), 10, fptr); // Reads 10 ints into
long position = ftell(fptr);
buffer
fscanf(): Formatted input (like scanf from a file).
fseek(): Moves the file pointer.
fscanf(fptr, "%d %s", &x, name);
fseek(fptr, 0, SEEK_SET); // Beginning of file