Presentation is loading. Please wait.

Presentation is loading. Please wait.

STANDARD INPUT/OUTPUT Lecture 9. Motivation Standard I/O refers to the places where most data is either read from, the keyboard, or written to, the video.

Similar presentations


Presentation on theme: "STANDARD INPUT/OUTPUT Lecture 9. Motivation Standard I/O refers to the places where most data is either read from, the keyboard, or written to, the video."— Presentation transcript:

1 STANDARD INPUT/OUTPUT Lecture 9

2 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

3 Basic I/O There are 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.

4 /* SIMPLEIO.C */ #include /* 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; }

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

6 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.

7 Table: Printf/scanf format characters Format Spec (%)TypeResult cCharSingle character i,dIntDecimal number OIntOctal number X,xIntHexadecimal number Lower/uppercase notation uIntUnsigned int

8 Table: Printf/scanf format characters Format Spec (%)TypeResult s*charPrint string Terminated by \ 0 fDouble/floatFormat –m.ddd... E, e"Scientific format -1.23e002 g,G"e or f whichever %-print % character

9 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.3f n", ); The output on the screen is: and: printf("VAT=17.5% n");...outputs: VAT=17.5%

10 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. 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);

11 CHARACTER STRING INPUT

12 /* Program - INTIN.C */ #include 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 %d\n", valin); } while (valin != 100); printf("End of program\n"); return 0; }

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

14 IN Memory I/O

15 /* Program - INMEM.C */ #include 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 %d %d %d\n", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]); printf("%s", line); sscanf(line,"%d %d %d %d %d", &result[4], &result[3], (result+2), (result+1), result); for (index = 0 ; index < 5 ; index++) printf("The final result is %d\n", result[index]); return 0; }

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

17 FILE INPUT/OUTPUT

18 /* Program - FORMOUT.C */ #include 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 %d\n", stuff, index); fclose(fp); /* close the file before ending program */ return 0; }

19 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

20 /* Program - CHAROUT.C */ #include 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 open\n"); 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; }

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

22 READING A FILE

23 /* Program - READCHAR.C */ #include int main() { FILE *funny; int c; funny = fopen("TENLINES.TXT", "r"); if (funny == NULL) { printf("File doesn't exist\n"); 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; }

24 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.

25 READING A WORD AT A TIME

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

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

28 HOW TO USE A VARIABLE FILENAME

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

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

31 HOW DO WE PRINT?

32 /* Program - PRINTDAT.C */ #include int main() { FILE *funny, *printer; int c; funny = fopen("TENLINES.TXT", "r"); /* open input file */ if (funny == NULL) { printf("File failed to open\n"); exit (EXIT_FAILURE); } printer = fopen("PRN", "w"); /* open printer file */ if (printer == NULL) { printf("Printer not available for use\n"); 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; }

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


Download ppt "STANDARD INPUT/OUTPUT Lecture 9. Motivation Standard I/O refers to the places where most data is either read from, the keyboard, or written to, the video."

Similar presentations


Ads by Google