Chapter 7 Files By C. Shing ITEC Dept Radford University.

38
Chapter 7 Files By C. Shing ITEC Dept Radford University

Transcript of Chapter 7 Files By C. Shing ITEC Dept Radford University.

Page 1: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Chapter 7 Files

By C. Shing

ITEC Dept

Radford University

Page 2: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 2

Objectives Understand how to use file utilities Understand how to use files by file pointers Understand how to use files by file descriptors Understand how to interact with operating

environment

Page 3: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 3

File A stream of bytes

Text file: user readable Binary file: machine readable, more efficient

Page 4: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 4

File Access Can access (by opening file first) by

File pointer: address of a structure FILE * defined in stdio.h Has buffer available

File descriptor: non-negative number represents file No data structure, flexible Needs to define buffer to hold more than 1 character No formatting specified

Page 5: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 5

File Utilities Create a temporary file

tmpnam (NULL) Returns a string of a temporary file name

Delete file remove (filenamestring);

Change filename rename (oldname string, newnamestring);

Example: char *tmpfile;

tmpfile=tmpnam(NULL);remove (tmpfile);

Page 6: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 6

Access File by File Pointer File pointer: declare for each file used

Pre-defined: stdin (keyboard input), stdout (output to screen),stderr (error message to screen)

Declared asFILE *filepointername;

Example:FILE *infile, *outfile;

Page 7: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 7

Access File by File Pointer - fopen Open file:

Form: fopen (“filename”, ”permission”) Returns a file pointer

Starts from beginning of the file Need to check successful when use fopen

Page 8: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 8

Access File by File Pointer – fopen (Cont.)

Open file: (Cont.) Permission:

Unix: both text and binary file r: read, for input w: write, for output

If file not exist, create it If file exists, erase file content

a: append to end of file, for updating If file not exist, create it

r+. w+: read and write

Page 9: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 9

Access File by File Pointer – fopen (Cont.) Open file: (Cont.)

Example:infile=fopen (“/usr/include/stdio.h”, “r”);

or outfile=fopen (“current_dir_file”, “w”); or outfile=fopen (argv[1], “a”);

Page 10: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 10

Access File by File Pointer – fopen (Cont.)

Open file: (Cont.) Permission: (Cont.)

MS-DOS Tex file: same as in Unix Binary file:

rb: read wb: write ab: append r+, w+: read and write

Page 11: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 11

Access File by File Pointer – fscanf, fprintf

Read/Write file: for text file Any data type

Input form:fscanf(inputfilepointer, “format”, variable_addr) Output form:fprintf(outputfilepointer, “format”,

variable_list)

Example:char character;while (fscanf(infile,”%c”,&character) != EOF)

fprintf (outfile, ”%c”, character);

Page 12: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 12

Access File by File Pointer – getc, putc Read/Write file:

Character Input form: getc (inputfilepointer) Output form: putc (character_variable, outputfilepointer)

Example:char character;while ((character=getc(infile)) != EOF)

putc (character, outfile);

Page 13: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 13

Access File by File Pointer – fgets, fputs Read/Write file:

String Input form: fgets (string, n, inputfilepointer)

Read at most n-1 characters into string from inputfile Output form: fputs (string, outputfilepointer)

Writes the string (except NULL character into outputfile

Example:

while (fgets(instring, n, infile) != EOF)

fputs (instring, outfile);

Page 14: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 14

Access File by File Pointer – fread, fwrite

Read/Write file: for binary file Input form: fread (arrayaddress, cellsize, n,

inputfilepointer)Read at most n*cellsize bytes into arrayaddress from inputfile

Output form: fwrite (arrayaddress, cellsize, n, outputfilepointer)

Writes at most n*cellsize bytes from arrayaddress into outputfile

Example:int arrayA[n];while (fread(arrayA, 4, n, infile) > 0)

fwrite (arrayA, 4, n, outfile);

Page 15: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 15

Access File by File Pointer – rewind Move pointer to the beginning of the file:

Form: rewind (filepointer);

Example:

rewind (infile);

Page 16: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 16

Access File by File Pointer – fseek Move pointer to any place of the file: fseek

Form: fseek (filepointer, offset, position); Offset: relative to position

-: to previous +: to next

Position: 0: beginning 1: current position 2: end

Example:fseek (outfile, 0, 2); // go to the end of the file

Page 17: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 17

Access File by File Pointer – ftell check the current file position: ftell

Form: ftell (filepointer);

Example:

while (ftell (infile)>0)

putc(getc(infile), outfile);

Page 18: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 18

Access File by File Pointer – feof check the end of file: feof

Form: feof (filepointer);

Example:

while (!feof (infile))

putc(getc(infile), outfile);

Page 19: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 19

Access File by File Pointer – fclose Form: fclose (filepointer);

Example:

fclose (infile);

fclose (outfile);

Page 20: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 20

Access File by File Pointer (Cont.) Class Example:

Example1

Page 21: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 21

Other Utilities Using File Pointer tmpfile(): returns a file pointer

Example:

FILE *tmpfileptr;

tmpfileptr=tmpfile();

putc(getc(infile),tmpfileptr);

Page 22: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 22

Access File by File Descriptor File descriptor: nonnegative integer for each file

Reserved: 0 (stdin), 1 (stdout), 2 (stderr) The rest files starts using 3, created when use open()

Page 23: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 23

Access File by File Descriptor - open

Open file: Starts from beginning of the file Defined in unistd.h (in MS-DOS, use io.h) check successful when use open Form: open (“filename”, options,

permission_octal) Returns a file descriptor

Page 24: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 24

Access File by File Descriptor – open (Cont.)

Example:

#include <fcntl.h>

#include <unistd.h>

int infilefd;

infilefd=open(argv[1],O_CREAT, 0400);

Page 25: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 25

Access File by File Descriptor – open (Cont.)

Open file: (Cont.) Permission: defined in /usr/include/fcntl.h,

use | to collect rights O_CREAT: create if not exists O_RDONLY: read only O_WRONLY: write only O_EXCL: give error if set O_CREAT and file exists O_RDWR: read and write

Page 26: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 26

Access File by File Descriptor – open (Cont.)

Open file: (Cont.) Permission: (Cont.)

O_APPEND: file pointer at file end O_TRUNC: if file exists, truncate file to empty O_NONBLOCK: not block for named pipe

Page 27: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 27

Access File by File Descriptor – open (Cont.)

Open file: (Cont.)

Example:infilefd=open (“/usr/include/stdio.h”,

O_RDONLY); or outfilefd=open (argv[2],

O_CREAT|O_WRONLY|O_TRUNC, 0600);

Page 28: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 28

Access File by File Descriptor – read, write

Read/Write file: for both text and binary files Input form: read (inputfd, bufferaddress, size)

Read at most size bytes into buffer from inputfile,

it returns numbers of bytes read Output form: write (outputfd, bufferaddress, size)

Prints at most size bytes from buffer address into

outputfile

Page 29: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 29

Access File by File Descriptor – read, write (Cont.)

Example:

int size;

char buffer[80];

while ((size=read(infilefd, buffer, 512)) > 0)

write (outfilefd, buffer, size);

Page 30: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 30

Access File by File Descriptor – lseek Move pointer to any place of the file: lseek

Form: lseek (filefd, offset, position); Offset (long): relative to position

-: to previous +: to next

Position: defined in /usr/include/stdio.h or

/usr/include/unistd.h 0 (or SEEK_SET): beginning 1 (or SEEK_CUR): current position 2 (or SEEK_END): end

Example:lseek (outfilefd, 0, 2); // go to the end of the file

Page 31: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 31

Access File by File Descriptor – close

Form: close (filedescriptor);

Example:

close (infilefd);

close (outfilefd);

Page 32: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 32

Access File by File Descriptor (Cont.)

Class Example:

Example 2

Page 33: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 33

Access File by File Descriptor (Cont.)

Example:

sparse.c

sparse.txt

normal.txt

Page 34: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 34

Interacting with Operating Environment Call Unix command

Use system tool Form: system (“Unix commands”);

Wait program

Page 35: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 35

Interacting with Operating Environment (Cont.) Send in output from Unix environment

using pipe Open pipe

Form: popen (“Unix command”, “permission”); Return a file pointer

Close pipe Use pclose(filepointer)

Page 36: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 36

Interacting with Operating Environment - system Example:

system (“vi myfile”);

Or

char *cmdstr;

sprintf(cmdstr, “vi %s”, argv[1]);

system (cmdstr);

Page 37: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 37

Interacting with Operating Environment – popen, pclose Example:

FILE *fileptr;

fileptr=popen (“find . -name myfile.c -print

| more”, “r”);

while ((character=getc(fileptr)) != EOF)

putc(character, stdout);

pclose(fileptr);

Page 38: Chapter 7 Files By C. Shing ITEC Dept Radford University.

Slide 38

References Deitel & Deitel: C How to Program, 4th ed.,

Chapter 11, Prentice Hall