[go: up one dir, main page]

0% found this document useful (0 votes)
74 views18 pages

Files Management: C Programming Unit 5

Files are stored on disks in the form of files, which can be text or binary. Common file operations include opening, reading, writing, and closing files. C provides functions like fopen(), fclose(), fprintf(), fscanf(), getc(), putc(), getw(), and putw() to perform operations on files. Fopen() is used to open a file and returns a file pointer. This pointer is then used as a parameter for other functions to perform read/write operations on the file. Fclose() closes the file when done.

Uploaded by

nandini p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views18 pages

Files Management: C Programming Unit 5

Files are stored on disks in the form of files, which can be text or binary. Common file operations include opening, reading, writing, and closing files. C provides functions like fopen(), fclose(), fprintf(), fscanf(), getc(), putc(), getw(), and putw() to perform operations on files. Fopen() is used to open a file and returns a file pointer. This pointer is then used as a parameter for other functions to perform read/write operations on the file. Fclose() closes the file when done.

Uploaded by

nandini p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Files Management

C PROGRAMMING  UNIT 5
Introduction 2
 So far we dealt with programs that store and process data in the computer’s primary
memory(RAM). How do we do the same with data in the secondary memory(disk).

 Data is stored in disks in the form of files. E.g. Your notepad files, word files, movie or
music files etc.

 Files are generally categorized into two: text files and binary files.

 Notepad file(notes.txt) or your program source code is text file where readable text is
stores. Video files, music files, executable program files etc. are examples of binary files.
3
 Following are the most common operations on files

o Open/Create

o Read

o Write

o Close
 C provides a number of functions that helps to perform basic file
operations.
4
Defining and Opening File(fopen( )) 5

 C provides a data type to work with files i.e. FILE. We need to create pointer of FILE
type before opening file.

 Function used create/open file is fopen(). This function on successful execution returns a
pointer to the opened file.

 fopen() takes two parameters, 1. The filename 2. The mode in which file is to
opened(more about it later)

E.g.: FILE *fp;

fp = fopen(“file name”, “mode”);


6

 On success, fopen() returns the address of the file


opened(call it as file handle)we store it in a FILE type
pointer fp. Now fp represents our file handle. Any further
operation we want to perform on the opened file requires
this file handle.
Parameters of fopen() 7

1. File name :

 The name of the file to be created/opened. Remember we will want to


create a file if it is not already existing. Otherwise we will want to open the
existing file. For both, fopen() is the function.

 Normally file name consists of the name part and an extension( e.g.
hello.txt). Extension is optional normally.
2. Mode:
 mode specifies the purpose of opening the file. 8
 Three major modes of opening a file : Read, Write, Append.
1. r: opens a text file in reading mode. You can only read data from file. The
file has to exist for this. Otherwise NULL is returned to indicate error.
2. w: Create and opens a text file in writing mode if it is not existing. If file
already exists, its contents are destroyed and opens it for writing fresh
content.
3. a: Opens a text file in append mode if it exists. Starts writing at the
end(append). Existing data is not destroyed. If the file does not exist, it
creates one and open for appending data(from start).
9
There are three additional modes which are variants of the above:
1. r+ opens an existing text file in both reading and writing mode. Start
reading/writing at the beginning.

2. w+ Creates new file if not existing. Opens a text file in both reading and
writing mode. Destroys the contents of existing file.

3. a+ Creates new file if not existing. Opens the file in both reading and
writing mode (append). Contents of existing file are not destroyed.
10

 E.g.
FILE *p1, *p2;
p1 = fopen("data","r");
p2 = fopen("results.txt","w");
Closing a file (fclose()) 11
 After we are finished with all operations, we are expected to close the file.
fclose () is the function/ for this. It takes file handle as the parameter.

 General form : fclose(file_pointer);

 E.g.: fclose(p1);
fclose(p2);
Input/Output Operations on File 12
getc() and putc()fuctions:
 The two simplest functions for I/O operations on files are: getc() &putc()
 Function putc(): Write a single character into the file specified.
 General Syntax: putc(c,fp);
 where c is a character variable and fp, the file pointer to the file opened in
write mode.

 Function getc(): Read a character from file specified and returns it.
 General Syntax : c = getc(fp);
 where c is a character variable and fp is file pointer to opened file.
#include <stdio.h>
int main() {
13
FILE *f1;
char c,d;
f1 = fopen("INPUT1", "w"); /* Open the file INPUT for writing*/
printf("Enter a character:");
c = getchar(); /* Get a character from keyboard */
putc(c,f1); /* Write into the file. */
fclose(f1); /* Close the file INPUT */
f1 = fopen("INPUT1","r"); /* Reopen the file INPUT1 */
d = getc(f1); /* Read back the character from file*/
printf("Data Output:");
printf("%c",d); /* Display a character on screen */
fclose(f1); /* Close the file*/
return 0; }
getw() and putw() functions 14
 Function putw(): Write a single integer into the file specified.
General Syntax: putw(number,fp);
where number is a integer variable and fp, the file pointer to the file opened in
write mode.

 Function getw(): Read a character from file specified and returns it.
General Syntax : num = getw(fp);
Where num is a integer variable and fp is file pointer to opened file.
#include <stdio.h>
int main() {
FILE *f1;
15
int number, num;
printf("Enter a integer:"); Output:
scanf("%d",&num); Enter ainteger:1234
// Open file for reading File data is: 1234
f1 = fopen("DATA1", "w"); /* Create DATA file */
// Write the integer to the file
putw(num,f1);
//closing the file
fclose(f1);
// Open the file for reading
f1 = fopen("DATA1", "r");
number = getw(f1); // Read the integer from file
printf("File data is: %d",number);
return 0; }
fprintf() and fscanf() functions 16
 Function fprintf(): Write specified formatted data into file
General Syntax: fprintf(fp , “control string”, data list);

where fp is the file pointer, control string is similar to printf function (“ e.g.:
%d %f %s”) and data list is the list of data item to be written to file.

List may contain variables or constants.


E.g. fprintf(f1, “%s %d %f”, name,age,7.5);
 Function fscanf(): Read data items from the file and store in to variables. 17
General Syntax : fscanf(fp , “control string”, list of variables);
where fp is the file pointer, control string is similar to scanf function (“ e.g.:
%d %f %s”) and variable list is the list of variable to which the read data to be
stored.

 Need to use address operator for single data item , as in case of scanf().
E.g. fscanf(f1, “%s %d”, item, &quantity);
#include <stdio.h> fp = fopen(filename, "r");

int main() { fscanf(fp, "%s %d %f", str2, &int2, &r2);


18
printf("File data is:\n");
FILE *fp;
printf("%s %d %f\n",str2, int2, r2);
int int1,int2;
fclose(fp);
float r1,r2;
return 0; }
char str1[20],str2[20], filename[20];
printf("Input file name\n"); Output:
scanf("%s", filename); Input file name file.txt
fp = fopen(filename, "w"); Enter a string, integer & float: Hello 1234 12.45

printf("Enter a string , integer & float:\n"); File data is: Hello 1234 12.450000

scanf("%s %d %f",str1, &int1, &r1);


fprintf(fp, "%s %d %f\n", str1, int1, r1);
fclose(fp);

You might also like