Presentation is loading. Please wait.

Presentation is loading. Please wait.

Professor Jodi Neely-Ritz University of Florida

Similar presentations


Presentation on theme: "Professor Jodi Neely-Ritz University of Florida"— Presentation transcript:

1 Professor Jodi Neely-Ritz University of Florida - 2004
canopen fl.dat Chapter 22 – part b /* Checks if a file can be opened for reading */ #include <stdio.h> main(int argc, char *argv[ ]) { . FILE *fp; if (argc != 2) { printf("usage: canopen filename\n"); return 2; } if ((fp = fopen(argv[1], "r")) == NULL) { printf("%s can't be opened\n", argv[1]); return 1; printf("%s can be opened\n", argv[1]); fclose(fp); return 0; The program will print: usage: canopen filename to remind the user that canopen requires a single file name. fl.dat can’t be opened fl.dat can be opened. Professor Jodi Neely-Ritz University of Florida

2 Professor Jodi Neely-Ritz University of Florida - 2004
Programs often need to create temporary files—files that exist only as long as the program is running. A compiler might first translate a C program to some intermediate form, which it stores in a file. <stdio.h> provides two functions, tmpfile and tmpnam, for working with temporary files. Professor Jodi Neely-Ritz University of Florida

3 Professor Jodi Neely-Ritz University of Florida - 2004
tmpfile creates a temporary file that exists until it's closed or the program ends. A call of tmpfile returns a file pointer that can be used to access the file later: FILE *tempptr; tempptr = tmpfile(); /* creates a temporary file */ If it fails to create a file, tmpfile returns a null pointer. tmpfile has a couple of drawbacks: don't know the name of the file that tmpfile creates can't decide later to make the file permanent. The alternative is to create a temporary file using fopen. tmpnam generates a name for a temporary file. If its argument is a null pointer, tmpnam stores the file name in a static variable and returns a pointer to it Professor Jodi Neely-Ritz University of Florida

4 Professor Jodi Neely-Ritz University of Florida - 2004
It is a very slow operation for a program to access a disk file directly each time it reads or writes a character, acceptable performance uses buffering. Data written to a stream is actually stored in a buffer area in memory; when it is full (or the stream is closed), the buffer is "flushed" (written to the actual output device). Input streams are buffered in a similar way: the buffer contains data from the input device; input is read from this buffer instead of the device itself. Buffering can result in enormous gains in efficiency, since one large "block move" is much faster than many tiny character moves. Professor Jodi Neely-Ritz University of Florida

5 Professor Jodi Neely-Ritz University of Florida - 2004
The functions in <stdio. h> perform buffering automatically. to take a more active role, use the functions fflush, setbuf, and setvbuf. When a program writes output to a file, the data normally goes into a buffer. The buffer is flushed automatically when it is full or the file is closed. By calling fflush a program can flush a file's buffer as often as it wishes. fflush(fp); /* flushes the buffer for the file associated with fp. */ fflush(NULL) ; /* flushes all buffers */ fflush returns zero if it is successful and EOF if an error occurs. Professor Jodi Neely-Ritz University of Florida

6 Professor Jodi Neely-Ritz University of Florida - 2004
setvbuf–changes how a stream is buffered, controls size and location of buffer. Called after a stream is opened and before any other operations are performed. Be sure to close the stream before its buffer is deallocated. setvbuf's 2nd argument: the address of the desired buffer. setvbuf's 3rd argument: specifies the kind of buffering desired. _IOFBF full buffering read from or write to a stream when empty or when full _IOLBF line buffering read from or write to a stream one line at a time. _IONBF no buffering directly read from or write to a stream without a buffer. setvbuf's last argument is the number of bytes in the buffer. larger buffer may give better performance, smaller buffer saves space setvbuf changes the buffering of stream to full buffering, using the N bytes in the buffer array as the buffer: char buffer[N] ; setvbuf(stream, buffer, _IOFBF, N) ; Professor Jodi Neely-Ritz University of Florida

7 Professor Jodi Neely-Ritz University of Florida - 2004
The functions remove and rename works with file names instead of file pointers, allows a program to perform basic file management operations. Both functions return zero if they succeed and a nonzero value if they fail. remove("foo") ; /* deletes the file named "foo" *./ If a program uses fopen (instead of tmpfile) to create a temporary file, it can use remove to delete the file before the program terminates. Be sure that the file to be removed has been closed. rename("foo", "bar"); /* renames "foo" to "bar" */ If the file to be renamed is open, be sure to close it before calling rename; a file can't be renamed if it is open. Professor Jodi Neely-Ritz University of Florida

8 Professor Jodi Neely-Ritz University of Florida - 2004
Difference between printf and fprintf is; printf always writes to stdout while fprintf writes to the stream indicated by its first argument: printf("Total: %d\n", total); /* writes to stdout */ fprintf(fp, "Total: %d\n", total); /* writes to fp */ printf, is equivalent to a call of fprintf with stdout as the first argument fprintf writes data to disk files and works well with any output stream. One of the most common uses of fprintf is to write error messages to stderr, the standard error stream. fprintf(stderr, "Error: data file can't be opened.\n"); Writing the message to stderr guarantees that it will appear on the screen even if the user redirects stdout. Professor Jodi Neely-Ritz University of Florida

9 Professor Jodi Neely-Ritz University of Florida - 2004
A printf conversion specification consists of the % character, followed by as many as five distinct items: Minimum field width (optional). An item too small to occupy this number of characters will be padded. (By default, spaces are added to the left of the item, thus right-justifying it within the field.) Precision is a period (.) followed by an integer or the character * If * is present, the precision is obtained from the next argument. If only the period is present, the precision is zero. Professor Jodi Neely-Ritz University of Florida

10 Professor Jodi Neely-Ritz University of Florida - 2004

11 Professor Jodi Neely-Ritz University of Florida - 2004

12 Professor Jodi Neely-Ritz University of Florida - 2004
fscanf and scanf read data items from an input stream, using a format string to indicate the layout of the input, then pointers follow as arguments. scanf always reads from stdin fscanf reads from the stream indicated by its first argument: scanf("%d%d", &i, &j); /* reads from stdin */ fscanf(fp, "%d%d" , &i, &j); /* reads from fp */ scanf is equivalent to a call of fscanf with stdin as first argument. The scanf functions return prematurely if : an input failure occurs, no more input characters could be read a matching failure occurs, input characters did not match the format string Both return the number of data items read and assigned to arguments; return EOF if input failure occurred before any data items could be read. Professor Jodi Neely-Ritz University of Florida


Download ppt "Professor Jodi Neely-Ritz University of Florida"

Similar presentations


Ads by Google