0% found this document useful (0 votes)
19 views

Basic File Operations

Uploaded by

gow
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Basic File Operations

Uploaded by

gow
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

BASIC FILE OPERATIONS

D. YASHWINI NAIK
23B81A05NM
CSE-J
Importance of File Operations in
Data Handling - File operations are
crucial for managing data that needs to
Definition of File be saved and retrieved beyond the
program's runtime. They enable data
Operations: File operations persistence, data transfer, and data
are actions performed on manipulation.
Types of Files -1. Text Files:
files, such as creating, Files that contain human-readable
opening, reading, writing, characters. Examples include .txt files.
and closing files. These 2. Binary Files: Files containing
binary data, which is not human-
operations allow programs to readable. Examples include .bin files.
store data persistently.
Example:
Opening and closing Files: FILE *filePointer;
Opening Files with “fopen”: filePointer=fopen("example.txt", "r");
if (filePointer == NULL) {
The “fopen” function is used to open a file. It requires two // Handle error
arguments: *The name of the file to open. printf("File could not be opened.\n");}
The mode in which to open the file (e.g., "r" for reading,
"w" for writing, “a" for appending).

Modes for Opening Files: Closing Files with “fclose”: The


*“r”: Opens a file for reading. “fclose” function is used to close an open file. It
The file must exist.
takes a single argument, which is the file
"w": Opens a file for writing.
. Creates the file if it doesn't exist. If it exists, the contents pointer.
are overwritten.
"a": Opens a file for appending. Creates the file if it Example Code Snippet:
doesn't exist. Writes data at the end of the file. FILE *filePointer;
"r+": Opens a file for both reading and writing. The file filePointer = fopen("example.txt", "r");
must exist.
if (filePointer != NULL) {
"w+": Opens a file for both reading and writing. Creates
the file if it doesn't exist. If it exists, the contents are // Perform file operations
overwritten. fclose(filePointer);
"a+": Opens a file for both reading and appending. }
Creates the file if it doesn't exist. Reads data from the else
beginning, and appends data at the end. {
printf("File could not be opened.\n");}
READING AND WRITING FILES:
READING FROM FILES:
“FSCANF”: READS FORMATTED INPUT FROM A FILE. “Fread”: Reads a block of data
EXAMPLE:
from a file.
FILE *FILEPOINTER = FOPEN("EXAMPLE.TXT", "R");
IF (FILEPOINTER != NULL) {
CHAR STR[100];
Example:
INT NUM;
FSCANF(FILEPOINTER, "%S %D", STR, &NUM);
FILE *filePointer =
PRINTF("READ STRING: %S, NUMBER: %D\N", STR,
fopen("example.bin", "rb");
NUM); FCLOSE(FILEPOINTER); if (filePointer != NULL) {
} char buffer[100];
“FGETS”: READS A LINE OF TEXT FROM A FILE. size_t bytesRead =
EXAMPLE: fread(buffer, sizeof(char), 100,
FILE *FILEPOINTER = FOPEN("EXAMPLE.TXT", "R"); filePointer); printf("Read
IF (FILEPOINTER != NULL) {
%zubytes\n", bytesRead);
CHAR BUFFER[100];
fclose(filePointer);
{
WHILE (FGETS(BUFFER, 100, FILEPOINTER) != NULL)
PRINTF("%S", BUFFER); } }
FCLOSE(FILEPOINTER);
}
WRITING TO FILES: fputs: Writes a string to a file.
Example:
• fprintf: Writes formatted output to a file. FILE *filePointer = fopen("example.txt",
"w");
• Example: if (filePointer != NULL) {
• FILE *filePointer = fopen("example.txt", "w"); fputs("Hello world\n", filePointer);
• if (filePointer != NULL) { fclose(filePointer);
}
• fprintf(filePointer, "Hello %s, %d\n", "world",
123); fclose(filePointer); fwrite: Writes a block of data to a file.
• } Example:
FILE *filePointer = fopen("example.bin",
"wb");
if (filePointer != NULL) {
char buffer[100] = "This is a test.";
size_t bytesWritten = fwrite(buffer,
sizeof(char), strlen(buffer), filePointer);
printf("Wrote %zu bytes\n", bytesWritten);
fclose(filePointer);
} 5
23B81A05NM
D.YASHWINI

THANK YOU CSE-J


NAIKNAIK
D.YASHWINI
23B81A05NM
CSE-J

You might also like