4 – File Processing

Post on 13-Jan-2016

33 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

4 – File Processing. Opening a File. Declare a file pointer: FILE *fp; Assign to a the file pointer: fp = fopen("clients.dat", "w"); Function prototype: FILE *fopen(char *name, char *mode);. fopen() Modes. - PowerPoint PPT Presentation

Transcript

1

Department of Computer Engineering Faculty of Engineering, Prince of Songkla University

4 – File Processing4 – File Processing

22

Opening a File

Declare a file pointer:FILE *fp;

Assign to a the file pointer:fp = fopen("clients.dat", "w");

Function prototype:FILE *fopen(char *name, char *mode);

33

fopen() Modes

Mode Meaning

"r" open text file for reading

"w" open text file for writing. If the file already exists, discard the contents.

"a" open text file for appending

44

Reading and Writing

of text files

reading and writing will be sequential

Writing clients.dat

Reading

55

Writing

/* write sequentially to clients.dat */

#include <stdio.h>int main(){ int acc; char name[10]; float bal; FILE *cfptr;

if ((cfptr = fopen("clients.dat","w")) == NULL) printf("File could not be opened\n"); else {

:

continued

66

printf("Enter acc, name, & bal.\n? "); scanf("%d%s%f", &acc, name, &bal);

while (!feof(stdin)) { fprintf(cfptr, "%d %s %.2f\n",

acc, name, bal); printf("? "); scanf("%d%s%f", &acc, name, &bal); } fclose(cfptr);}return 0;

}

77

clients.dat

1 Davison 23.672 Paun 0.03356 Mason 89.0145 Jackson 11.00

:

88

Reading

/* read sequentially from clients.dat */#include <stdio.h>

int main(){ int acc; char name[10]; float bal; FILE *cfptr;

if((cfptr = fopen("clients.dat", "r")) == NULL) printf("File could not be opened\n"); else {

:

continued

99

printf("Acc Name Bal\n"); fscanf(cfptr, "%d %s %f",

&acc, name, &bal);

while(!feof(cfptr)) { printf("%d %s %f\n", acc,name,bal); fscanf(cfptr, "%d %s %f",

&acc, name, &bal); } fclose(cfptr); } return 0;}

1010

Example: Double Space a File

Call:

$ dbl_space file1 file2

dbl_space.c

double_space()

prn_info()

A Graceful fopen()

1111

dbl_space.c

#include <stdio.h>#include <stdlib.h>

void double_space(FILE *, FILE *);void prn_info(char *);

int main(int argc, char *argv[]){ FILE *ifp, *ofp;

if (argc != 3){ prn_info(argv[0]); exit(1); }

:

continued

1212

ifp = fopen(argv[1], "r"); /* read */ ofp = fopen(argv[2], "w"); /* write */

double_space(ifp, ofp);

fclose(ifp); fclose(ofp);

return 0;}

1313

double_space()

void double_space(FILE *ifp, FILE *ofp){ int c;

while ((c = getc(ifp)) != EOF) { putc(c, ofp); if (c == ' ') putc(' ', ofp); /* found space -- double it */ }}

1414

prn_info()

void prn_info(char *pgn_name)/* version 1 */{ printf("\nUsage: %s infile outfile\n", pgn_name); }

void prn_info(char *pgn_name)/* version 2 */{ fprintf(stderr, "\nUsage: %s inf outf\n",

pgn_name);}

1515

A Graceful fopen()

FILE *gfopen(char *file_name, char *mode){ FILE *fp;

if ((fp = fopen(file_name, mode)) == NULL) { fprintf(stderr, "\nCannot open %s\n", file_name); exit(1); } return fp;}

1616

Random Access

Insertion Problem

How to do Random Access

Binary Filesfwrite()

Creation Programfseek()

Manipulation Programfread()

Printing Program

1717

Insertion Problem

clients.dat:

0 White 12.5630 Davison 2345.7832 Deitel 0.00

:

1002 Paun 0.0323 White 12.5135 Brown 1.45

insertion using sequential read/write is very expensive

1818

How to do Random Access

Creation Program

Create a binary file called creditaccount.dat using fwrite() where each entry is a fixed length.

::

::

::

::

1

2

3

100

Davison Andrew 12.27

White Jim 3.24

Brown Mary 23.67

Wai Loke Seng 4.45

acctnum lastname firstname bal

1919

Manipulation Program

Use fseek() with acctnum to go to an entry immediately.

2020

Binary Files

A binary file stores the system's internal representation of C data structures.

fopen() modes must include a "b": "rb", "wb", "ab", etc.

2121

fwrite()

Informally:fwrite( <pointer to data being inserted>,

<size of the data type>,<number of data items being inserted>,<file pointer>)

Example:fwrite( &blankclient,

sizeof(struct clientdata),1, cfptr);

2222

Creation Program

/* create 100 empty clients in clientaccount.dat */

#include <stdio.h>

struct clientdata { int acctnum; char lastname[15]; /* size specified */ char firstname[10]; /* size specified */ float balance;};

:

continued

2323

int main(){ int i; struct clientdata blankclient =

{0, "", "", 0.0}; FILE *cfptr;

if ((cfptr =fopen("creditaccount.dat", "wb"))==NULL) printf("File could not be opened.\n"); else {

:

continued

2424

for (i = 1; i <= 100; i++) fwrite( &blankclient,

sizeof(struct clientdata), 1, cfptr);

fclose (cfptr); } return 0;}

2525

fseek()

Function prototype:int fseek(FILE *fp, long int offset,

int whence);

offset is the position of the entry.

In this case, it is:(acctnum of entry - 1) * size of an entry

2626

whence is a symbolic constant indicating the starting position for a seek:

SEEK_SET beginning of file

SEEK_CUR current location in file

SEEK_END end of file

2727

Manipulation Program

/* insert client info for a specified accnum into creditaccount.dat */

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

struct clientdata { int acctnum; char last[15]; char first[10]; float bal;};

:

continued

2828

int main(){ FILE *cfptr; struct clientdata client;

if ((cfptr =fopen("creditaccount.dat", "rb+")) == NULL)

printf("File could not be opened.\n"); else {

:

continued

2929

scanf("%d%s%s%f",&client.acctnum, client.last, client.first, &client.bal);

fseek(cfptr, (client.acctnum - 1) * sizeof(struct clientdata), SEEK_SET);

fwrite(&client, sizeof(struct clientdata), 1, cfptr);

} fclose(cfptr); return 0;}

3030

fread()

Informally:fread( <pointer to data structure for

holding extracted data>,<size of the data type>,<number of data items>,<file pointer>)

Example:fread( &client,

sizeof(struct clientdata),1, cfptr);

3131

Printing Program

/* print out creditaccount.dat */

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

struct clientdata { int acctnum; char last[15]; char first[10]; float bal;};

:

continued

3232

int main(){ FILE *cfptr; struct clientdata client;

if ((cfptr = fopen("creditaccount.dat", "rb")) == NULL)

printf("File could not be opened.\n"); else {

:

continued

3333

printf("Acct Lastname Firstname Balance\n"); while (!feof(cfptr)) { fread(&client,

sizeof(struct clientdata), 1, cfptr);

if (strcmp(client.last, "") != 0) printf("%d %s %s %f\n",

client.acctnum, client.last, client.first, client.bal);

} } fclose(cfptr); return 0;}

3434

Text Files and Binary Files Compared

Example Data Structures

Writing to a Text File

Writing to a Binary File

When to Use a Binary File

When to Use a Text File

3535

Example Data Structures

#define PNUM 50 /* number of planets */#define NAMELEN 20

struct planet { char name[NAMELEN]; /* known size */ double diameter; double dist_sun;};struct planet earth;struct planet planets[PNUM];

/* initialise earth and planets */

3636

Writing to a Text File

Assume a text file is being accessed by the file pointer tp:

fprintf(tp, "%s %lf %lf", earth.name, earth.diameter, earth.dist_sun);

for (i = 0; i < PNUM; ++i)fprintf(tp, "%s %lf %lf",

planets[i].name, planets[i].diameter, planets[i].dist_sun);

3737

Writing to a Binary File

Assume a binary file is being accessed by the file pointer bp:

fwrite(&earth, sizeof(planet), 1, bp);

fwrite(planets, sizeof(planet)*PNUM, PNUM, bp);

3838

When to Use a Binary File

If the file will contain complicated data structures.

If the file is to be manipulated using byte size-related functions (fwrite(), fseek(), etc).

3939

If speed of access is important(use fseek() instead of a sequential search).

If storage space is important (the data structure size can be defined).

4040

When to Use a Text File

If the file will contain text.

If the file is to be moved between systems (text is portable).

top related