File handling assignment
File handling assignment
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22281 Submission date:
Title:
Problem
Statement
Objectives:
Theory:
Concept So far the operations using the C program are done on a
prompt/terminal which is not stored anywhere. The output
is deleted when the program is closed. But in the software
industry, most programs are written to store the
information fetched from the program .
Different possible operations that we can perform on a file
in C are:
1. Creating a new file – fopen() with attributes as “a” or
“a+” or “w” or “w+”
2. Opening an existing file – fopen()
3. Reading from file – fscanf() or fgets()
4. Writing to a file – fprintf() or fputs()
5. Moving to a specific location in a file – fseek(),
rewind()
6. Closing a file – fclose()
P: f-LTL-UG/03/R1 Page 1 of 8
Functions for C File Operations:-
P: f-LTL-UG/03/R1 Page 2 of 8
2) rb - Open for reading in binary mode. If the file
does not exist, fopen( ) returns NULL .
3) w - Open for writing in text mode. If the file exists,
its contents are overwritten. If the file doesn’t
exist, a new file is created. Returns NULL, if
unable to open the file.
4) wb - Open for writing in binary mode. If the file
exists, its contents are overwritten. If the file does
not exist, it will be created .
5) a- Searches file. If the file is opened successfully
fopen( ) loads it into memory and sets up a pointer
that points to the last character in it. It opens only
in the append mode. If the file doesn’t exist, a new
file is created. Returns NULL, if unable to open the
file.
6) ab- Open for append in binary mode. Data is
added to the end of the file. If the file does not
exist, it will be created.
7) r+ - Searches file. It is opened successfully fopen(
) loads it into memory and sets up a pointer that
points to the first character in it. Returns NULL, if
unable to open the file.
8) rb+ - Open for both reading and writing in binary
mode. If the file does not exist, fopen( ) returns
NULL.
9) w+ - Searches file. If the file exists, its contents are
overwritten. If the file doesn’t exist a new file is
created. Returns NULL, if unable to open the file.
10) wb+ - Open for both reading and writing in
binary mode. If the file exists, its contents are
overwritten. If the file does not exist, it will be
created.
P: f-LTL-UG/03/R1 Page 3 of 8
11) a+ - Searches file. If the file is opened
successfully fopen( ) loads it into memory and sets
up a pointer that points to the last character in it. It
opens the file in both reading and append mode. If
the file doesn’t exist, a new file is created. Returns
NULL, if unable to open the file .
12) ab+ - Open for both reading and appending in
binary mode. If the file does not exist, it will be
created.
As given above, if you want to perform operations on a
binary file, then you have to append ‘b’ at the last. For
example, instead of “w”, you have to use “wb”, instead of
“a+” you have to use “a+b”. File Pointer is used in almost
all the file operations in C.
Syntax of File Pointer:-
FILE *pointername;
FILE * fptr;
fptr = fopen(“fileName.txt”, “r”);
P: f-LTL-UG/03/R1 Page 4 of 8
fscanf(fptr, "%s %s %s %d", str1, str2, str3, &year);
char c = fgetc(fptr);
Write to a File:-
The file write operations can be performed by the
functions fprintf() and fputs() with similarities to read
operations. Example of a code snippet is as follows:-
FILE *fptr ;
fptr = fopen(“fileName.txt”, “w”);
fprintf(fptr, "%s %s %s %d", "We", "are", "in", 2012);
fputc("a", fptr);
Closing a File:-
The fclose() function is used to close the file. After
successful file operations, you must always close a file to
remove it from the memory.
Example of code snippet is as follows :-
FILE *fptr ;
fptr= fopen(“fileName.txt”, “w”);
---------- Some file Operations -------
fclose(fptr);
#include <stdio.h>
#include <string.h>
int main()
{
FILE *filePointer;
char dataToBeWritten[50] = "Coding is amazing";
if (filePointer == NULL) {
P: f-LTL-UG/03/R1 Page 5 of 8
printf(" File opened.");
}
else {
printf("Coding is interesting.\n");
if (strlen(dataToBeWritten) > 0) {
fclose(filePointer);
return 0;
}
int main()
{
FILE *filePointer;
char dataToBeRead[50];
filePointer = fopen("College.c", "r");
if (filePointer == NULL) {
printf("College.c file failed to open.");
}
else {
printf("%s", dataToBeRead);
}
fclose(filePointer);
P: f-LTL-UG/03/R1 Page 6 of 8
printf( "Data successfully read from file \n");
printf("The file is now closed.");
}
return 0;
}
CONCLUSION:
P: f-LTL-UG/03/R1 Page 7 of 8
REFERENCES: refer lab manual for the same
P: f-LTL-UG/03/R1 Page 8 of 8