Post on 18-Jan-2016
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
Slide 3
File A stream of bytes
Text file: user readable Binary file: machine readable, more efficient
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
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);
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;
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
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
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”);
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
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);
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);
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);
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);
Slide 15
Access File by File Pointer – rewind Move pointer to the beginning of the file:
Form: rewind (filepointer);
Example:
rewind (infile);
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
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);
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);
Slide 19
Access File by File Pointer – fclose Form: fclose (filepointer);
Example:
fclose (infile);
fclose (outfile);
Slide 20
Access File by File Pointer (Cont.) Class Example:
Example1
Slide 21
Other Utilities Using File Pointer tmpfile(): returns a file pointer
Example:
FILE *tmpfileptr;
tmpfileptr=tmpfile();
putc(getc(infile),tmpfileptr);
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()
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
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);
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
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
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);
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
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);
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
Slide 31
Access File by File Descriptor – close
Form: close (filedescriptor);
Example:
close (infilefd);
close (outfilefd);
Slide 32
Access File by File Descriptor (Cont.)
Class Example:
Example 2
Slide 33
Access File by File Descriptor (Cont.)
Example:
sparse.c
sparse.txt
normal.txt
Slide 34
Interacting with Operating Environment Call Unix command
Use system tool Form: system (“Unix commands”);
Wait program
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)
Slide 36
Interacting with Operating Environment - system Example:
system (“vi myfile”);
Or
char *cmdstr;
sprintf(cmdstr, “vi %s”, argv[1]);
system (cmdstr);
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);
Slide 38
References Deitel & Deitel: C How to Program, 4th ed.,
Chapter 11, Prentice Hall