cse1301 computer programming: lecture 6 input/output

36
CSE1301 Computer Programming: Lecture 6 Input/Output

Upload: winfred-gallagher

Post on 19-Jan-2016

243 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSE1301 Computer Programming: Lecture 6 Input/Output

CSE1301 Computer Programming:

Lecture 6Input/Output

Page 2: CSE1301 Computer Programming: Lecture 6 Input/Output

Topics

• Streams

• Formatted input

• Formatted output

Reading: Deitel and Deitel: Section 9.1 to 9.5, Section 9.11

Page 3: CSE1301 Computer Programming: Lecture 6 Input/Output

Recall:

scanf()

Example:scanf(“%d”, &x);

printf()

Example:printf(“The value of x is %d\n”, x);

#include <stdio.h>

Page 4: CSE1301 Computer Programming: Lecture 6 Input/Output

Input/Output

Program

Page 5: CSE1301 Computer Programming: Lecture 6 Input/Output

Streams

• Text input or output is dealt with as streams of characters.

• A stream serves as a channel to convey characters between I/O and programs.

Page 6: CSE1301 Computer Programming: Lecture 6 Input/Output

Streams: Input

135 25.5_

1 3 5 2 5 . 5 \n

Example 1:

int item;

float cost;

scanf(“%d %f”, &item, &cost);

input buffer

Page 7: CSE1301 Computer Programming: Lecture 6 Input/Output

Streams: Input

135 25.5_

1 3 5 2 5 . 5 \n

Example 1:

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item cost

Page 8: CSE1301 Computer Programming: Lecture 6 Input/Output

Streams: Input

135 25.5_

2 5 . 5 \n

Example 1:

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item

135

cost

Page 9: CSE1301 Computer Programming: Lecture 6 Input/Output

Streams: Input

135 25.5_

\n

Example 1:

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item

135

cost

25.5

Page 10: CSE1301 Computer Programming: Lecture 6 Input/Output

Streams: Output

H e l l o ! \n

Example 2: printf(“Hello!\n”);

output buffer

Page 11: CSE1301 Computer Programming: Lecture 6 Input/Output

H e l l o ! \n

Example 2: printf(“Hello!\n”);

Streams: Output

Page 12: CSE1301 Computer Programming: Lecture 6 Input/Output

Example 2:

Hello!_

printf(“Hello!\n”);

Streams: Output

Page 13: CSE1301 Computer Programming: Lecture 6 Input/Output

Streams

• From the program's point of view, the characters are queued in a pipe.

• The sequence of characters are organised into lines.

• Each line:

– can have zero or more characters;

– ends with the "newline" character '\n' .

Page 14: CSE1301 Computer Programming: Lecture 6 Input/Output

"Standard" Streams • stdin - standard input

– usually from keyboard

• stdout - standard output– usually to screen

• stderr - standard error (unbufferd)– usually to screen

• must have at the top of your program#include <stdio.h>

• these streams can be redirected (more on this later)

Page 15: CSE1301 Computer Programming: Lecture 6 Input/Output

stdin: Input

• Data is read in from stdin (into a variable) using the scanf() function.

• When input ends, the scanf() function returns a special value: EOF.

Page 16: CSE1301 Computer Programming: Lecture 6 Input/Output

Example: ReadData

input name, age, gender, idNumber

Page 17: CSE1301 Computer Programming: Lecture 6 Input/Output

#include <stdio.h>

/*************************************\

Read in important info about a lecturer

\**************************************/

main()

{

char name[100] ;

float age ;

char gender ;

int idNumber ;

scanf("%s %f %c %d", name, &age, &gender, &idNumber);

}

Input: Joey 19.5 M 666

Joey

19.5

M

666

Page 18: CSE1301 Computer Programming: Lecture 6 Input/Output

stdout:Output

• Data is written out to stdout (eg. from a variable) using the printf() function.

Page 19: CSE1301 Computer Programming: Lecture 6 Input/Output

Example: WriteData

set name to ”Joey"

set age to 19.5

set gender to ’M'

set idNumber to 666

output name, age, gender, idNumber

Page 20: CSE1301 Computer Programming: Lecture 6 Input/Output

#include <stdio.h>

/*****************************************\

Write out important info about a lecturer

\*****************************************/

main()

{

char *name = ”Joey" ;

float age = 19.5;

char gender = ’M';

int idNumber = 666 ;

printf("%s\n%f\n%c\n%d\n", name, age, gender, idNumber);

}

Joey19.5M666_

Page 21: CSE1301 Computer Programming: Lecture 6 Input/Output

Formatted Input and Output

• General form:

printf(format-control-string, other-arguments);

scanf(format-control-string, other-arguments);

• Examples:

printf(s\n%f\n%c\n%d\n", name, age, gender,idNumber);

scanf("%s %f %c %d", name, &age, &gender, &idNumber);

Page 22: CSE1301 Computer Programming: Lecture 6 Input/Output

• Describes the format of the data for output

• Contains “conversion specifiers” and “literal characters”

Example:

printf(“%s is %d years old.\n”, name, age);

Format-Control-String

Page 23: CSE1301 Computer Programming: Lecture 6 Input/Output

• Describes the format of the data for output

• Contains “conversion specifiers” and “literal characters”

Example:

printf(“%s is %d years old.\n”, name, age);

conversion specifiers

Format-Control-String

Page 24: CSE1301 Computer Programming: Lecture 6 Input/Output

Example:

printf(“%s is %d years old.\n”, name, age);

literal characters

Format-Control-String

• Describes the format of the data for output

• Contains “conversion specifiers” and “literal characters”

Page 25: CSE1301 Computer Programming: Lecture 6 Input/Output

• For printf: variables containing data for output.

Example:

printf(“(“%s%s is %d%d years old.\n”, namename, ageage);

Other-Arguments: printf

Page 26: CSE1301 Computer Programming: Lecture 6 Input/Output

• For scanf: “pointers” to variables in which input will be stored.

scanf("%s %f %c %d", name, &age, &gender,&id);

Other-Arguments: scanf

Page 27: CSE1301 Computer Programming: Lecture 6 Input/Output

• Variables of type int, float or char need ‘&’

scanf("%s %f %c %d", name, &age, &gender,&id);

Other-Arguments: scanf

• Do not use ‘&’ with strings!

• ‘&’ is for scanf only!

• For scanf: “pointers” to variables in which input will be stored.

Page 28: CSE1301 Computer Programming: Lecture 6 Input/Output

Common Conversion Specifiers

• string: %sprintf(“Name: %s\n”, name);

scanf(“%s”, name);

• char: %cprintf(“What letter follows %c?\n”, ch);

scanf(“%c”, &nextchar);

• decimal integers: %dprintf(“What is %d plus %d?\n”, x, y);

scanf(“%d”, &sum);

Page 29: CSE1301 Computer Programming: Lecture 6 Input/Output

Common Conversion Specifiers

• float: %fprintf(“%f squared is...? ”, x);

scanf(“%f”, &ans);

• double:printf(“%f squared is...? ”, x);

scanf(“%lf”, &ans);

Page 30: CSE1301 Computer Programming: Lecture 6 Input/Output

scanf: Conversion specifiers

• d: read an optionally signed decimal integer. • i: read an optionally signed decimal, octal, or

hexadecimal integer. • i and d: The corresponding argument is a

“pointer” to integer.

int idNumber;

scanf("%d", &idNumber) ;

Page 31: CSE1301 Computer Programming: Lecture 6 Input/Output

scanf: Conversion specifiers

• h or l: place before any of the integer conversion specifiers to indicate that a short or long integer is to be input.

long int idNumber;

scanf("%ld", &idNumber) ;

• Also: i, o, u, x, e, f, g... [Fig 9.17 D&D]

Page 32: CSE1301 Computer Programming: Lecture 6 Input/Output

Conversion example

input octal integer

output integer as decimal

Page 33: CSE1301 Computer Programming: Lecture 6 Input/Output

Conversion example

#include <stdio.h>

main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

}

Page 34: CSE1301 Computer Programming: Lecture 6 Input/Output

Conversion example

#include <stdio.h>

main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

}

7056_

i

56

Page 35: CSE1301 Computer Programming: Lecture 6 Input/Output

Skipping characters in input stream

• Skipping blank spaces

scanf("%d %d %d", &day, &month, &year);

• An alternative– Enter data as dd-mm-yyyy: 16-3-1999 – Store each number in date

scanf("%d-%d-%d", &day, &month, &year);

Page 36: CSE1301 Computer Programming: Lecture 6 Input/Output

Summary

• Input from keyboard is via the stdin stream• Output to the screen is via the stdout stream • Streams carry characters

– divided into lines with ‘\n’ character– input ends with special value: EOF

• To use the C library functions available, you must include the stdio.h header file.

• Input and output can be formatted and converted between data types.