Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files A collection of related data treated as a unit. Two types Text

Similar presentations


Presentation on theme: "Files A collection of related data treated as a unit. Two types Text"— Presentation transcript:

1 Files A collection of related data treated as a unit. Two types Text
Binary Stored in secondary storage devices. Buffer Temporary storage area that holds data while they are being transferred to or from memory.

2 Text Files Data are stored as human-readable characters
Each line of data ends with a newline character

3 Standard Files Standard input Associated with the keyboard
Accessed using stdin Standard output Associated with the monitor Accessed using stdout Standard error Accessed using stderr

4 User Files External files defined by the user
Must be explicitly opened in the program. C assigns a logical name to each external file. fopen function Prepares a file for processing Makes the connection between the external file and the program Creates a program file table or control structure to store the information needed to process the file

5 fopen FILE * fopen(char * filename, char * mode)
filename – string that supplies the name of the file as known to the external world. e.g. scores.dat mode Meaning r Open file for reading If file exists, the marker is positioned at beginning. If file does not exist, error returned w Open text file for writing If file exists, it is emptied. If file does not exist, it is created. a Open text file for append. If file exists, the marker is positioned at the end.

6 Examples

7 fopen If succesful, will return a pointer to a FILE object
If not, a null pointer is returned Always check to make sure the open was successful. If not, print an error message and exit Example:

8 fclose int fclose(FILE *fp) Used to close a file when no longer needed
Prevents associated file from being accessed again Guarantees that data stored in the stream buffer is written to the file Releases the FILE structure so that it can be used with another file Frees system resources, such as buffer space Returns zero on success, or EOF on failure

9 fclose Examples:

10 Formatted Input Functions
Read and convert a stream of characters and store the converted values in a list of variables found in the address list scanf scanf ("format string", address list); Reads text data from standard input fscanf fscanf(fp, "format string", address list); Reads input from the specified file

11 Formatted Output Functions
Displays output in human readable form printf printf ("format string", value list); Writes to standard output or standard error file fprintf fprintf (fp, "format string", value list); Writes to the specified file.

12 Formatting Input/Output Functions
Format strings Consist of three types of data Whitespace Text characters Field specification These may be repeated

13 Field Specification Consists of % character, a conversion code, and other formatting instructions With one exception (*), each field specification must have a matching parameter in the parameter list that follows the string The type in the field specification and the type of the parameter must match Can have up to six elements for output Can have up to five elements for input; precision is not allowed

14 Field Specification Elements of field specification scanf / fscanf:
% flag Max width size conversion code * do not store data h short l long int l double (scan only) L long double left justify + sign (+ or -) space if positive 0 zero padding d, f, c, s, e, E, x, X, i, u, g, G % flag Max width precision size conversion code printf / fprintf

15 Field Specification Examples %d %7d %+d %lf %7.2lf

16 End of File Controlled Loops
feof int feof(FILE *fp) Function to check if end of file has been reached. For an end of file controlled loop Read before the loop Test for end of file: while (!feof(ptr)) Inside loop: Process Read at the bottom of the loop


Download ppt "Files A collection of related data treated as a unit. Two types Text"

Similar presentations


Ads by Google