DOC PREVIEW
MIT 16 070 - Study References

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fesq, 3/19/01 1 16.070Input/Output3/19/01 Lecture #17 16.070• All useful programs perform some form of Input/Output (I/O)• We have been using unformatted I/O to read from keyboard and writeto screen• Other I/O options are available! Formatted I/O to control format of data! Batch I/O to read and write to files instead of keyboard/screenFesq, 3/19/01 2 16.070Input/Output• In C, I/O is performed by calling library functions! The printf function− Performs output to standard output device (monitor)− Requires format string which provides text/values to print out and specificationson how to print them out− Manages the process of converting the bit pattern into proper sequence of ASCIIcharacters based on formal spec provided by programmer! The scanf function− Performs input from standard input device (keyboard)− Requires format string and list of addresses of variables into which the valuesretrieved from the input device will be stored− Converts the input according to conversion characters in format string and assignsconverted values to the contents of the variables listedFesq, 3/19/01 3 16.070Standard Input/Output• Using printf or scanf, data can be formatted in may different ways! Scientific notation! Right justified! Left justified! Filled with a fill character! Specify number of positions! Etc.• Refer to Chapter 7 in Uckan to determine how to accomplish theseformatsFesq, 3/19/01 4 16.070Unformatted vs Formatted Output• With unformatted output, values are printed in minimum predefinedamount of space, beginning at current cursor positionprintf ("%f", 0.511); " 0.511000printf ("%f", 365.3); " 365.3000printf ("%d", 42); " 42• To specify field width, insert decimal constant before conversioncharacterprintf ("%8d", 42); " 42! Default pad character is a space! To make default pad character a 0, set first digit of field width to 0printf ("%08d", 42); " 00000042Fesq, 3/19/01 5 16.070Format Specifiers for Formatted Output• Format specifiers (a.k.a. Flag Characters) are optional format modifiersthat specify alignment, +/- and scientific notation! - Left Justificationprintf ("%-8d", 42); "42^^^^^^! + Prefix number with + or - (default is no + for positive numbers)printf ("%+8d", 42); "^^^^^+42! (space) Prefix number with space or - (default is no space for positivenumbers)printf ("% 8d", 42); "^^^^^^42! Precision - specify number of places to print after decimal pointprintf ("%10.1f", 0.511); " ^^^^^^^0.5! Scientific Notationprintf ("%10.3e", 0.511); " ^5.110e-01Fesq, 3/19/01 6 16.070Non-interactive I/O - Batch Programs• Not all programs interact with a user! IRS tax return audit program• Instead of using keyboard input and monitor output, data can be readfrom a data file and written to a data file• Programs that access files and perform non-interactive I/O are calledbatch programsFesq, 3/19/01 7 16.070Streams• C performs I/O through streams that are associated with files ordevices• A stream is an abstraction for a flow of data from a source to a sink• A stream consists of an ordered series of bytes• Reading/writing to a file or device involves reading from/writing to astream• Three standard streams are automatically opened for every C program! stdin - Standard Input Stream, usually the keyboard! stdout - Standard Output Stream, usually the monitor! stderr - Standard Error Stream, usually the monitorFesq, 3/19/01 8 16.070File Input/Output - Declaring Files• Some programs require ability to read/write ASCII data files• To perform I/O operations, associate a stream with a file or device! Declare a pointer to a structure type called FILE (all caps!). FILE isdefined within header file stdio.hFormat:FILE *<internal_name>; /* identify a stream name*/FILE *test_data; /*test_data is internal file name*/! This creates a file pointer -- pointer to the FILE structure, which providesoperating system with bookkeeping information! FILE structure contains fields to hold information such as file's name,access mode, and pointer to next character in the stream (file positionindicator)! File pointer maps a particular stream to a particular file or device! Use file pointer to read from, write to, or close the streamFesq, 3/19/01 9 16.070File Input/Output - Opening Files! Before you can read from or write to a file, you must open it with thefopen() function! fopen() maps the file pointer to a physical file/device. For files, mapsexternal file name to internal file name! Specify how the file will be accessed; e.g., read, write, appendFormat:<internal_name> = fopen("<external_name>", "<access_mode>");Example:test_data = fopen("simresults.dat", "r");/*open simresults.dat as read only*/! This establishes test_data as an output stream between program andexternal file simresults.datFesq, 3/19/01 10 16.070File Input/Output - Read/Write• Once file is opened, use file pointer to perform read/write operations! One object at a time - fscanf(), fprintf()! One character at a time - fgetc(), fputc()! One line at a time - fgets(), fputs()− fgets() reads characters until it reaches new line, end-of-file, or maximumnumber of characters specified− fputs() writes to the file and then inserts a new-line character! One block at a time - fread(), fwrite()Fesq, 3/19/01 11 16.070File Input/Output - Closing Files• Files that are opened in a program should be closed after processing iscomplete! fclose() cuts the connection between the program and the fileFormat:fclose(<internal_name>);Example:fclose(test_data); /*close file that test_data waspointing to: i.e., simresults.dat*/! File pointer no longer exists. Program cannot access file until it has beenre-openedFesq, 3/19/01 12 16.070File Input/Output - Error Handling• Each I/O function returns a special value if an error occurs! If successful, fopen() function returns a file pointer to the physical filethat can be used to access the file later in the program. If not successful,returns null.! It is a good practice to check that fopen() was successfulFILE *test_data;test_dat = fopen("simresults.dat", "r");if (test_data == NULL)printf("Error opening simresults.dat\n");! Caution: Error value varies from one function to another. Some functionsreturn zero for error, others return non-zero valueFesq, 3/19/01 13 16.070File Input/Output - Error Handling (cont.)• A stream's error status can be checked via pre-defined error functions! feof() checks whether an end-of-file was encountered during


View Full Document

MIT 16 070 - Study References

Documents in this Course
optim

optim

20 pages

Load more
Download Study References
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study References and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study References 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?