Presentation is loading. Please wait.

Presentation is loading. Please wait.

Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,

Similar presentations


Presentation on theme: "Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,"— Presentation transcript:

1 Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Files in C

2 Two Kinds of File Access
Stream File is treated as a sequence of bytes Access is sequential – i.e., in byte order Cannot replace data in the middle of a file Raw File is a sequence of blocks Any block can be read and/or written independently CS-2303, A-Term 2012 Files in C

3 Definition – File A (potentially) large amount of information or data that lives a (potentially) very long time Often much larger than the memory of the computer Often much longer than any computation Sometimes longer than life of machine itself (Usually) organized as a linear array of bytes or blocks Internal structure is imposed by application (Occasionally) blocks may be variable length (Often) requiring concurrent access by multiple threads or processes Even by processes on different machines! CS-2303, A-Term 2012 Files in C 3

4 Implementations of Files
Usually on disks (or devices that mimic disks) Magnetic – hard drive or floppy Optical – CD, DVD Flash drives – electronic memory, organized as disks Requirement Preserve data contents during power-off or disasters Directory / Folder Special kind of file that contains links pointing to other files Associates names with files CS-2303, A-Term 2012 Files in C 4

5 Opening and Closing Files
FILE *fopen(char *name, char *mode) Makes a file available for use mode may be "r ", "w", "a", etc. If mode == "w" or "a", file is created if it does not already exist. mode == "w"  overwrite file from beginning mode == “a"  add to end of file int fclose(FILE *fp); Disconnects file from program Flushes output buffers, cleans up internal data structures CS-2303, A-Term 2012 Files in C

6 Stream File Access Declared in <stdio.h> fgetc(), fgets(), fputc(), fputs(), fscanf(), fprintf(), fopen(), fclose(), … Familar tools fread(), fwrite(), fseek(), ftell(), rewind(), fgetpos(), fsetpos() Not so familiar Note:– if you seek to a position in a file and start writing, file is truncated at that point All take FILE * argument to identify the file. CS-2303, A-Term 2012 Files in C

7 Standard Streams FILE *stdin; FILE *stdout; FILE *stderr;
stdin, stdout, stderr are extern variables declared in <stdio.h> FILE is typedef declared in <stdio.h> FILE *stdin; Whatever shell or window system connects to stream named stdin E.g., keyboard E.g., file redirection:– command < filename scanf(…) is same as fscanf(stdin, …) FILE *stdout; Whatever shell or window system connects to stream named stdout E.g., window or screen E.g., file redirection:– command > filename printf(…) is same as fprintf(stdout, …) FILE *stderr; Usually the window or screen; may be redirected to a file may be redirected to file using command 2> filename Opened by OS before program starts Redirections may be combined CS-2303, A-Term 2012 Files in C

8 Raw File Access See Kernighan & Ritchie, Chapter 8 Raw file access
Without simplifying stream functions – e.g., scanf, fscanf, printf, fprintf, fgetc, etc. read and write raw disk blocks Seek to a file position lseek, fseek — sets file pointer to specified location Subsequent read, write, etc., start there ftell – returns file pointer CS-2303, A-Term 2012 Files in C 12

9 Raw File Access (continued)
See Chapter 8 for more details Streams are a layer of abstraction on top of raw access See K&R §8.1–8.6 for example implementations CS-2303, A-Term 2012 Files in C

10 Questions? CS-2303, A-Term 2012 Files in C


Download ppt "Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,"

Similar presentations


Ads by Google