standard input/output lecture 9. motivation standard i/o refers to the places where most data is...

Post on 01-Apr-2015

214 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

• standard header for input/output:#include <stdio.h>

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.

/* 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 yang Anda ketik.)

Program selesai.

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,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 by \ 0

f Double/float Format –m.ddd...

E, e " Scientific format

-1.23e002

g,G " e or f whichever

% - print % character

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",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....) -- 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);

CHARACTER STRING INPUT

/* 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 %d\n", valin); } while (valin != 100);

printf("End of program\n");

return 0;}

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

• /* 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 %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;}

Result of execution

74 18 33 30 97

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

FILE INPUT/OUTPUT

/* 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 %d\n", 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 is an example line. Line number 1This is an example line. Line number 2This is an example line. Line number 3This is an example line. Line number 4This is an example line. Line number 5This is an example line. Line number 6This is an example line. Line number 7This is an example line. Line number 8This is an example line. Line number 9This is an example line. Line number 10

/* 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 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;}

Result of output (appended to TENLINES.TXT)

Additional lines.Additional lines.Additional lines.Additional lines.Additional lines.Additional lines.Additional lines.Additional lines.Additional lines.Additional lines.

READING A FILE

/* 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 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;}

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

READING A WORD AT A TIME

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

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;}

Result of executionThisisanexampleline.Linenumber1Thisisan ... (Many other lines) ...Additionallines.Additionallines.lines.

HOW TO USE A VARIABLE FILENAME

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

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;}

Result of execution

(The file selected is listed on the monitor)

HOW DO WE PRINT?

/* 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 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;}

Result of execution

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

top related