DOC PREVIEW
MIT 16 070 - Recitation 7

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 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 8 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 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

16 070 Introduction to Computers and Programming March 22 Recitation 7 Spring 2001 Topics Input Output Data Conversion Formatting Output with printf File Input Output Analog vs Digital Analog Digital Digital Analog Input Output Formatting Output with printf There are several characters that can be added to the format specifiers d c f lf e and s used in printf in order to achieve special formatting in the output The table below contains the characters that can be added between the symbol and the character in the format specifier and their resulting format Character integer 0 decimal Example 10d 10d 010d 10d 10 3f Special Formatting prints the data in a field that is 10 spaces wide prints the data left justified prints the data with zeros filling the field to the left prints positive data with a plus sign prints the data with a field width of 10 with 3 decimal places If your program uses many printf statements and you want all of your data formatted the same way you can make use of variables for determining field width or precision For example if we define a variable width to represent the field width that we want and a variable precision to represent the number of decimal places that we want It is possible to write our output statements as follows printf f width precision data Using this format allows you to change multiple output statements at once File Input Output In order to use file I O you must declare every input and output file with the following syntax FILE file name This declares the internal file name for the file you want to operate on sometimes called a file handle Next you must point this internal file name to an external file using the fopen command The format of the fopen command is as follows file name fopen external file name Openmode Where Openmode is one of the following Open Mode r Resulting Permissions read access w a r w a write access append data to the end of an existing file opens a file for input output if it exists already the contents are not destroyed destroys the file if it exists and creates a new one for update opens a file for update so that writing is done at the end of the file The w and w modes are destructive The a and a modes are not destructive They append data to already existing files The difference is that you can read from a file that was opened with a but not one that was opened with a If the fopen command fails for some reason the function will return a NULL pointer value This can be useful when determining whether a file has opened successfully or not After opening and operating on a file it must be closed This is done with the fclose command flcose file name Another very useful function is the feof function It returns a true 1 if the most recent input operation on the file returned an end of file character and returns false 0 otherwise There is also a constant EOF that is defined to represent the end of file character This can also be used in various ways to test for the end of a file Writing Data to Files The fprintf function is used to write data to a file It is very similar to the printf function except it requires a file name to write to fprintf file name format specifiers variables There are two other functions that can be used to write to files The fputc function writes a character to a file and the fputs function writes a string to a file fputc CharacterExpression file name fputs StringExpression file name Reading Data from Files The functions used to read data from files are similar to those seen earlier The three functions are fscanf fgetc and fgets fscanf file name format specifiers variables fgetc file name fgets StringExpression Size file name Since fgetc has no variable specification it is usually used in an expression in the form of CharacterVariable fgetc file name Input from Preformatted Data Files The data can be contained in fixed width fields it is possible to read it by simply using the proper width declaration in the format specifiers of the fscanf function The data can also be separated by a specific character such as a comma in a comma separated value CSV file It is possible to store the separation character in a dummy variable or include the character between the specifiers in the function call Example of file I O TJ March 2001 Recitation 7 File I O This program writes integers to a file and reads the same integers from it again include stdio h int main void int a b c declare 3 integer variables to write to file declare file handle internal name FILE file name open file file name fopen test file txt w print to file fprintf file name d 1 fprintf file name d 2 fprintf file name d 3 close file fclose file name re open file file name fopen test file txt r read from file fscanf file name d a fscanf file name d b fscanf file name d c close file fclose file name print contents to screen printf n File contains d d d n n a b c return 0 end main Data Conversion Reference Data and Computer Communications by William Stallings 4th Ed MacMillan Publisher This section deals mostly with modulation techniques involved when actually transferring communicating analog information via a digital format or when transferring communicating digital information via an analog format i e so called modulation techniques This is different from the actual digital to analog and analog to digital conversion processes which entail converting single digital numbers to analog outputs and converting an analog input to single sampled digital values respectively An example of simple analog to digital conversion A sensor outputs a voltage in the range 0 to 10V signifying the rotational angle of a solar array If your computer needs to know the solar array angle it reads the sensor voltage at a specific time and converts the voltage between 0 and 10 to a digital value between say 0 and 255 if the value is represented by a single byte An example of simple digital to analog conversion A DC motor s angular velocity needs to be controlled with a voltage input between 0 and 12V Your computer computes the angular velocity required of the motor The motor can be controlled by a circuit converting a digital number say 0 to 255 8 bits again to an analog voltage scaled between 0 and 12V In general the difference between modulation de modulation techniques and actual direct analog to digital A D and digital to analog D A conversion is that modulation techniques are concerned with conveying information by employing specific signal patterns as you will see while


View Full Document

MIT 16 070 - Recitation 7

Documents in this Course
optim

optim

20 pages

Load more
Download Recitation 7
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 Recitation 7 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 Recitation 7 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?