• No results found

lec02-23iitkdgp.ppt

N/A
N/A
Protected

Academic year: 2020

Share "lec02-23iitkdgp.ppt"

Copied!
15
0
0

Loading.... (view fulltext now)

Full text

(1)

File Handling in C

(2)

Introduction

Files are places where data can be stored

permanently.

Some programs expect the same set of data to

be fed as input every time it is run.

Cumbersome.

Better if the data are kept in a file, and the

program reads from the file.

Programs generating large volumes of output.

Difficult to view on the screen.

Better to store them in a file for later viewing/

(3)

Basic File Operations

Opening a file

Reading data from a file

Writing data to a file

(4)

Opening a File

A file must be

“opened”

before it can be used.

FILE *fp;

:

fp = fopen (filename, mode);

fp

is declared as a pointer to the data type FILE.

filename

is a string - specifies the name of the file.

fopen

returns a pointer to the file which is used in all

subsequent file operations.

mode

is a string which specifies the purpose of opening

the file:

“r” :: open the file for reading only

“w” :: open the file for writing only

(5)

Contd.

Points to note:

Several files may be opened at the same

time.

For the “w” and “a” modes, if the named file

does not exist, it is automatically created.

(6)

Examples

FILE *in, *out ;

in = fopen (“mydata.dat”, “r”) ;

out = fopen (“result.dat”, “w”);

FILE *empl ;

char filename[25];

scanf (“%s”, filename);

(7)

Closing a File

After all operations on a file have been

completed, it must be closed.

Ensures that all file data stored in memory buffers

are properly written to the file.

General format:

fclose (file_pointer) ;

FILE *xyz ;

(8)

Read/Write Operations on Files

The simplest file input-output (I/O) function are

getc

and

putc

.

getc

is used to read a character from a file and return it.

char ch; FILE *fp;

…..

ch = getc (fp) ;

getc

will return an end-of-file marker EOF, when the end of the

file has been reached.

putc

is used to write a character to a file.

char ch; FILE *fp;

……

(9)

Example :: convert a text file to all

UPPERCASE

main() {

FILE *in, *out ;

char c ;

in = fopen (“infile.dat”, “r”) ;

out = fopen (“outfile.dat”, “w”) ;

while ((c = getc (in)) != EOF)

putc (toupper (c), out);

(10)

Contd.

We can also use the file versions of

scanf

and

printf

, called

fscanf

and

fprintf

.

General format:

fscanf (file_pointer, control_string, list) ;

fprintf (file_pointer, control_string, list) ;

Examples:

(11)

Some Points

How to check EOF condition when using

fscanf

?

Use the function

feof

if (feof (fp))

printf (“\n Reached end of file”) ;

How to check successful open?

For opening in “r” mode, the file must exist.

(12)

Example

typedef struct {

int roll;

char dept_code[6];

float cgpa;

} STUD;

main() {

FILE *stud;

STUD s;

float sum = 0.0;

int count = 0;

stud = fopen (“stud.dat”, “r”) ;

while (1) {

if (feof (stud)) break;

fscanf (stud, “%d %s %f”, &s.roll,

s.dept_code, &s.cgpa);

count ++;

sum += s.cgpa;

}

printf (“\nThe average cgpa is %f”,

sum/count);

fclose (stud);

(13)

Arguments to main ()

Command line arguments are parameters supplied to

a program, when the program is invoked.

cc myfile.c

cc xyz.c -lm

netscape www.mailcity.com

average 10 20 30 40 50

How do these parameters get into the program?

Every C program has a

main

function.

main

can take two arguments conventionally called

argc

and

(14)

Echoing the command line

arguments

int main (int argc, char *argv[])

{

int i;

printf (“argc = %d\n”, argc) ;

for (i=0; i<argc; ++i)

printf (“argv[%d] = %s\n”,

i,argv[i]) ;

return 0;

}

$ a.out how many

argc = 3

(15)

Example :: convert a text file to all

UPPERCASE

, using command line

arguments

main (int argc, char *argv[ ] {

FILE *in, *out ;

char c ;

in = fopen (argv[1], “r”) ;

out = fopen (argv[2], “w”) ;

while ((c = getc (in)) != EOF)

putc (toupper (c), out);

fclose (in) ;

fclose (out) ;

Run this program as:

References

Related documents

Sheet using excel that if named field is declare the new content has been merged into one command, declare the first elseif statement is great for windows username incorrect..

liability is assessed and the payment of Tax is made upon the income earned in previous year is known as Assessment Year.

If breastfeeding by itself doesn’t effectively remove the thickened inspissated milk, then manual expression of the milk, or the use of an efficient breast pump after feeds will

The paper argues that there is a breakup of community social structures as evident from changes in family relations in critical domains such as extra-marital relations, sexual

signature for appliances. The total power consumption is continuously monitored, and step changes in the power are matched with the appliance database. Real power was used as a

Integral bridges are characterized by monolithic connection between the deck and the substructure (piers and abutments).. They span

Like sync solutions, MagFS enables access to enterprise data from remote locations and mobile devices and provides the workforce with simple file sharing and col-