Download presentation
Presentation is loading. Please wait.
Published byHarvey Shaw Modified over 9 years ago
1
CSCI 171 Presentation 12 Files
2
Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines of characters Each line has 0 – 255 characters ending in a carriage return Carriage return is converted to operating systems end of line character Used for text files –Binary stream – data read and written unchanged (in binary format) Used for binary files
3
File names Names must adhere to operating system rules –8.3 for DOS –256 characters for Windows 95, 98, NT –256 for most UNIX systems –Letters, Numbers, and Special characters are allowed in names
4
File names Strings are used for file names Recall the slash (\) has special meaning to C Examples: char * fileName = “c:\\winnt\\myfile.txt”; char * fileName = “c:/winnt/myfile.txt”;
5
Opening files You cannot use a file until it has been opened The fopen function is located in stdio.h –Accepts 2 argument: filename (string) mode (string) –Returns a file pointer
6
Available modes What mode a file is opened in dictates how the file is opened –Files may be opened for reading or writing –Files may be opened in text mode or binary mode
7
Available Text Modes ModeMeaningSpecial Conditions rReadingReturns NULL if file does not exist wWritingIf file does not exist, it is created If file exists, data is erased aAppendingIf file does not exist, it is created If file exists, data appended to end *Modes are available for to open a file for read and write
8
Available Binary Modes ModeMeaningSpecial Conditions rbReadingReturns NULL if file does not exist wbWritingIf file does not exist, it is created If file exists, data is erased abAppendingIf file does not exist, it is created If file exists, data appended to end *Modes are available for to open a file for read and write
9
The fopen function #include void main( void ) { FILE * fptr; char * fileName = "c:/temp/test.txt"; char * mode = "r"; if ((fptr = fopen(fileName, mode)) == NULL) { printf("Opening file %s caused an error.", fileName); exit(1); } //Processing continues }
10
Types of files Two main types –Text files Can be viewed by user Read by other applications (db, spreadsheet, other programs in other languages, etc.) –Binary files Cannot be viewed by user Read by other programs
11
Types of files Three main structures to data files –Specified number of records Usually specified in first record for loops –Trailer or sentinel signal Usually specified in last record while loops –End-of-file indicator Every file contains a system-readable end-of-file feof function in conjunction with while loops
12
Text file output Functions in stdio.h –fprintf most common –fputs –putc no fputc must include file name
13
fprintf fprintf works like the printf function –fprintf allows you to tell the compiler what file you are using –printf automatically uses stdout prototype comparison: int fprintf(FILE *fp, char *fmt, …); int printf(char *fmt, …);
14
fprintf example #include int main( void ) { FILE * fp; int x = 3; if ((fp = fopen("c:/test.txt", "w")) == NULL) { printf("Error opening file!"); exit(1); } fprintf(fp, "The value of x is: %d", x); return 0; }
15
Text file input Functions in stdio.h –fscanf most common –fgets –getc and fgetc identical must include file name
16
fscanf fscanf works like the scanf function –fscanf allows you to tell the compiler what file you are using –scanf automatically uses stdout prototype comparison: int fscanf(FILE *fp, char *fmt, …); int scanf(char *fmt, …);
17
fscanf example //Assume test.txt is a text file with an integer in it: #include int main( void ) { FILE * fp; int x = 0; if ((fp = fopen("c:/test.txt", "r")) == NULL) { printf("Error opening file!"); exit(1); } fscanf(fp, "%d", &x); printf("The value of x is: %d", x); return 0; }
18
Binary file output fwrite() function prototype: –fwrite(void *buf, int size, int count, FILE *fp) buf points to the data size tells the size of the objects to be written count tells how many objects are to be written fp tells which file is being written to
19
fwrite example #include int main( void ) { FILE * fp; int x[5] = {1, 2, 3, 4, 5}; if ((fp = fopen("c:/test.dat", "wb")) == NULL) { printf("Error opening file!"); exit(1); } fwrite(x, sizeof(int), 5, fp); return 0; }
20
Binary file input fread() function prototype: –fread(void *buf, int size, int count, FILE *fp) buf points to the data size tells the size of the objects to be read count tells how many objects are to be read fp tells which file is being read from
21
fread example #include int main( void ) { FILE * fp; int x[5], i; if ((fp = fopen("c:/test.dat", "rb")) == NULL) { printf("Error opening file!"); exit(1); } fread(x, sizeof(int), 5, fp); for (i = 0; i < 5; i++) printf("%d ", x[i]); return 0; }
22
Closing and flushing files All files should be flushed and closed prior to program termination –flushing writes the data from the buffer to the actual file files written in blocks, streams written in characters do data is buffered prior to being written –closing literally closes the file – needs to be reopened to be used again –all files are closed by the compiler at program termination, but this process should be handled by the programmer
23
fclose fclose will flush and close a file –prototype: int fclose(FILE *fp); fcloseall will flush and close all but the standard files –Prototype int fcloseall( ); –Not ANSI standard –will not close stdout, stdin, etc.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.