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

File handling assignment

File handeling pdf

Uploaded by

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

File handling assignment

File handeling pdf

Uploaded by

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

PUNE INSTITUTE OF COMPUTER TECHNOLOGY

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:

Write as per experiment list.

Problem
Statement

Refer lab manual for below


Prerequisites:

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:-

File opening modes/creating modes in C:-


File opening modes or access modes specify the allowed
operations on the file to be opened. They are passed as an
argument to the fopen() function. Some of the commonly
used file access modes are listed below:
1) r- Searches file. If the file is opened successfully
fopen( ) loads it into memory and sets up a pointer
that points to the first character in it. If the file
cannot be opened fopen( ) returns NULL .

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;

So the file can be opened as,


pointername = fopen("filename.txt", "w");

Reading From a File:-


The file read operation in C can be performed using
functions fscanf() or fgets(). Both the functions performed
the same operations as that of scanf and gets but with an
additional parameter, the file pointer.So it depends on u
whether u want read the code line by line or character by
character , and the example of code snippet for reading
file is:-

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);

Code:- FOR WRITE ONLY MODE:-

#include <stdio.h>
#include <string.h>

int main()
{

FILE *filePointer;
char dataToBeWritten[50] = "Coding is amazing";

filePointer = fopen("College.c", "w");

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) {

// writing in the file using fputs()


fputs(dataToBeWritten , filePointer);
fputs("\n", filePointer);
}

fclose(filePointer);

printf("Data successfully written \n");


printf("The file is now closed.");
}

return 0;
}

FOR READ ONLY MODE:-


#include <stdio.h>
#include <string.h>

int main()
{

FILE *filePointer;
char dataToBeRead[50];
filePointer = fopen("College.c", "r");

if (filePointer == NULL) {
printf("College.c file failed to open.");
}
else {

printf("The file is now opened.\n");

while (fgets(dataToBeRead, 50, filePointer)


!= NULL) {

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;
}

Output FOR WRITE ONLY MODE:-

FOR READ ONLY MODE:-

CONCLUSION:

P: f-LTL-UG/03/R1 Page 7 of 8
REFERENCES: refer lab manual for the same

Continuous Assessment for DS AY: 2023-24


RPP (5) SPO (5) Total (10) Signature:
Assessed By:
Start date Submission date Date:

*Regularity, Punctuality, performance


*Submission, Presentation, orals

P: f-LTL-UG/03/R1 Page 8 of 8

You might also like