STANDARD INPUTOUTPUT Lecture 9 Motivation Standard IO refers

  • Slides: 33
Download presentation
STANDARD INPUT/OUTPUT Lecture 9

STANDARD INPUT/OUTPUT Lecture 9

Motivation • Standard I/O refers to the places where most data is either read

Motivation • Standard I/O refers to the places where most data is either read from, the keyboard, or written to, the video monitor. • standard header for input/output: #include <stdio. h>

Basic I/O • There a couple of function that provide basic I/O facilities. •

Basic I/O • There a couple of function that provide basic I/O facilities. • Probably the most common are: getchar() and putchar(). They are defined and used as follows: – int getchar(void) -- reads a char – int putchar(char ch) -- writes a char, returns character written.

 /* SIMPLEIO. C */ #include <stdio. h> /* standard header for input/output */

/* SIMPLEIO. C */ #include <stdio. h> /* standard header for input/output */ int main() { int c; printf(“Masukkan sembarang karakter, X = program berhenti. n"); do { c = getchar(); /* get a single character from the kb */ putchar(c); /* display the character on the monitor */ } while (c != 'X'); /* until an X is hit */ printf("Program selesai. n"); return 0; }

Result of execution Masukkan sembarang karakter, X = program berhenti. (Output tergantung pada apa

Result of execution Masukkan sembarang karakter, X = program berhenti. (Output tergantung pada apa yang Anda ketik. ) Program selesai.

Printf • The function is defined as follows: – int printf(char *format, arg list.

Printf • The function is defined as follows: – int printf(char *format, arg list. . . ) -- prints to stdout the list of arguments according specified format string. Returns number of characters printed. • The format string has 2 types of object: – ordinary characters -- these are copied to output. – conversion specifications -- denoted by % and listed in Table below.

Table: Printf/scanf format characters Format Spec (%) Type Result c Char Single character i,

Table: Printf/scanf format characters Format Spec (%) Type Result c Char Single character i, d Int Decimal number O Int Octal number X, x Int Hexadecimal number Lower/uppercase notation u Int Unsigned int

Table: Printf/scanf format characters Format Spec (%) Type Result s *char Print string Terminated

Table: Printf/scanf format characters Format Spec (%) Type Result s *char Print string Terminated by 0 f Double/float Format –m. ddd. . . E, e " Scientific format -1. 23 e 002 g, G " e or f whichever % - print % character

Between % and format char we can put: - (minus sign) -- left justify.

Between % and format char we can put: - (minus sign) -- left justify. integer number -- field width. m. d -- m = field width, d = precision of number of digits after decimal point or number of chars from a string. So: printf("%-2. 3 f n", 17. 23478); The output on the screen is: 17. 235 and: printf("VAT=17. 5%% n"); . . . outputs: VAT=17. 5%

scanf This function is defined as follows: int scanf(char *format, args. . ) --

scanf This function is defined as follows: int scanf(char *format, args. . ) -- reads from stdin and puts input in address of variables specified in args list. Returns number of chars read. Format control string similar to printf Note: The ADDRESS of variable or a pointer to one is required by scanf(``%d'', &i); We can just give the name of an array or string to scanf since this corresponds to the start address of the array/string. char string[80]; scanf(``%s'', string);

CHARACTER STRING INPUT

CHARACTER STRING INPUT

 /* Program - INTIN. C */ #include <stdio. h> int main() { int

/* Program - INTIN. C */ #include <stdio. h> int main() { int valin; printf(“Masukkan angka dari 0 sampai 32767, stop jika 100. n"); do { scanf("%d", &valin); /* read a single integer value in */ printf("The value is %dn", valin); } while (valin != 100); printf("End of programn"); return 0; }

Result of execution Masukkan angka dari 0 sampai 32767, stop jika 100. (The output

Result of execution Masukkan angka dari 0 sampai 32767, stop jika 100. (The output depends on the numbers you type in. ) End of program

IN Memory I/O

IN Memory I/O

 • /* Program - INMEM. C */ #include <stdio. h> int main() {

• /* Program - INMEM. C */ #include <stdio. h> int main() { int numbers[5], result[5], index; char line[80]; numbers[0] = 74; numbers[1] = 18; numbers[2] = 33; numbers[3] = 30; numbers[4] = 97; sprintf(line, "%d %d %dn", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]); printf("%s", line); sscanf(line, "%d %d %d", &result[4], &result[3], (result+2), (result+1), result); for (index = 0 ; index < 5 ; index++) printf("The final result is %dn", result[index]); return 0; }

Result of execution 74 18 33 30 97 The final result is 30 The

Result of execution 74 18 33 30 97 The final result is 30 The final result is 33 The final result is 18 The final result is 74

FILE INPUT/OUTPUT

FILE INPUT/OUTPUT

 /* Program - FORMOUT. C */ #include <stdio. h> #include <string. h> int

/* Program - FORMOUT. C */ #include <stdio. h> #include <string. h> int main() { FILE *fp; char stuff[25]; int index; fp = fopen("TENLINES. TXT", "w"); /* open for writing */ strcpy(stuff, "This is an example line. "); for (index = 1 ; index <= 10 ; index++) fprintf(fp, "%s Line number %dn", stuff, index); fclose(fp); /* close the file before ending program */ return 0; }

Result of execution (The following is written to the file named TENLINES. TXT) This

Result of execution (The following is written to the file named TENLINES. TXT) This is an example line. Line number 1 This is an example line. Line number 2 This is an example line. Line number 3 This is an example line. Line number 4 This is an example line. Line number 5 This is an example line. Line number 6 This is an example line. Line number 7 This is an example line. Line number 8 This is an example line. Line number 9 This is an example line. Line number 10

 /* Program - CHAROUT. C */ #include <stdio. h> #include <string. h> #include

/* Program - CHAROUT. C */ #include <stdio. h> #include <string. h> #include <stdlib. h> int main() { FILE *point; char others[35]; int indexer, count; strcpy(others, "Additional lines. "); point = fopen("tenlines. txt", "a"); /* open for appending */ if (point == NULL) { printf("File failed to openn"); exit (EXIT_FAILURE); } for (count = 1 ; count <= 10 ; count++) { for (indexer = 0 ; others[indexer] ; indexer++) putc(others[indexer], point); /* output one character */ putc('n', point); /* output a linefeed */ } fclose(point); return EXIT_SUCCESS; }

Result of output (appended to TENLINES. TXT) Additional lines.

Result of output (appended to TENLINES. TXT) Additional lines.

READING A FILE

READING A FILE

 /* Program - READCHAR. C */ #include <stdio. h> #include <stdlib. h> int

/* Program - READCHAR. C */ #include <stdio. h> #include <stdlib. h> int main() { FILE *funny; int c; funny = fopen("TENLINES. TXT", "r"); if (funny == NULL) { printf("File doesn't existn"); exit (EXIT_FAILURE); } else { do { c = getc(funny); /* get one character from the file */ putchar(c); /* display it on the monitor */ } while (c != EOF); /* repeat until EOF (end of file) */ } fclose(funny); return EXIT_SUCCESS; }

Result of execution This is an example line. Line number 1 This is an

Result of execution This is an example line. Line number 1 This is an example line. Line number 2 This is an example line. Line number 3 This is an example line. Line number 4 This is an example line. Line number 5 This is an example line. Line number 6 This is an example line. Line number 7 This is an example line. Line number 8 This is an example line. Line number 9 This is an example line. Line number 10 Additional lines.

READING A WORD AT A TIME

READING A WORD AT A TIME

/* Program - READTEXT. C */ #include <stdio. h> int main() { FILE *fp

/* Program - READTEXT. C */ #include <stdio. h> int main() { FILE *fp 1; char oneword[100]; int c; fp 1 = fopen("TENLINES. TXT", "r"); do { c = fscanf(fp 1, "%s", oneword); /* get one word from file */ printf("%sn", oneword); /* display it on the monitor */ } while (c != EOF); /* repeat until EOF */ fclose(fp 1); return 0; }

Result of execution This is an example line. Line number 1 This is an

Result of execution This is an example line. Line number 1 This is an . . . (Many other lines). . . Additional lines.

HOW TO USE A VARIABLE FILENAME

HOW TO USE A VARIABLE FILENAME

 /* Program - ANYFILE. C */ #include <stdio. h> #include <stdlib. h> int

/* Program - ANYFILE. C */ #include <stdio. h> #include <stdlib. h> int main() { FILE *fp 1; char oneword[100], filename[25]; char *c; printf("Enter filename -> "); scanf("%s", filename); /* read the desired filename */ fp 1 = fopen(filename, "r"); if (fp 1 == NULL) { printf("File failed to openn"); exit (EXIT_FAILURE); } do { c = fgets(oneword, 100, fp 1); /* get one line from the file */ if (c != NULL) printf("%s", oneword); /* display it on the monitor */ } while (c != NULL); /* repeat until NULL */ fclose(fp 1); return EXIT_SUCCESS; }

Result of execution (The file selected is listed on the monitor)

Result of execution (The file selected is listed on the monitor)

HOW DO WE PRINT?

HOW DO WE PRINT?

/* Program - PRINTDAT. C */ #include <stdio. h> #include <stdlib. h> int main()

/* Program - PRINTDAT. C */ #include <stdio. h> #include <stdlib. h> int main() { FILE *funny, *printer; int c; funny = fopen("TENLINES. TXT", "r"); /* open input file */ if (funny == NULL) { printf("File failed to openn"); exit (EXIT_FAILURE); } printer = fopen("PRN", "w"); /* open printer file */ if (printer == NULL) { printf("Printer not available for usen"); exit (EXIT_FAILURE); } do { c = getc(funny); /* get one character from the file */ if (c != EOF) { putchar(c); /* display it on the monitor */ putc(c, printer); /* print the character */ } } while (c != EOF); /* repeat until EOF (end of file) */ fclose(funny); fclose(printer); return 0; }

Result of execution (The file named TENLINES. TXT is listed on the printer, and

Result of execution (The file named TENLINES. TXT is listed on the printer, and it is listed on the monitor. )