Download presentation
Presentation is loading. Please wait.
Published byCassandra Thornley Modified over 10 years ago
1
Internship in DASAN networks C programing language Chapter 7 : Input and Output Present by Le Thi Hien 1/14
2
Contents Standard Input and Output Variable-length Argument List Formatted Output Formatted Input File Access Error Handling Line Input and Output 2/14
3
Standard Output and Input Standard input (stdin) : data going into a programe. typical from keyboard. Standard output (stdout): the stream where a program writes its output data. 3/14
4
Standard Input and Output Standard input/ output can be substituted by File : Input :Prog <infile Output :Prog >outfile Example : ./a.out >result.c ./a.out <result.c Pipeline : Otherprog | prog Prog | anotherprog 4/14
5
Variable-length Argument Lists Syntax : type name (arg1,…); Example : void minprintf(char *fmt,…) { va_list ap; va_start(ap, fmt);/*ap point to 1 st unnamed arg*/ … case ‘d’ : ival = va_arg(ap, int); … va_end(ap); } -Need #include - va_list type - Macro : va_start(va_list ap, lastfix); va_arg(va_list ap, type); va_end(va_list ap); 5/14
6
Fomatted Output SyntaxUseReturn valueExample int printf(const char *format,…); Write data to stdout as the format argument specifies -Success : total number of characters written -Fail : negative number printf(“%d\n”, a); int fprintf(FILE* stream, const char* format,…); Write data to the specified stream as the format argument specifies fprintf(stdout, “%s\n”,c); int sprintf(char (str, const char *format,…); Write data to stringsprintf(s, “%s if %d”, a, b); 6/14
7
Formatted Input SyntaxUseReturn valueExample Int scanf(const char* format, …); Read data from stdin and store according to the format into the location pointed by additional argument - Success : the number of items successfully read - Fail : EOF Scanf(“%d”, &a) Int fscanf(FILE* stream, const char*format,…); Read formatted data from stream FILE *fp = fopen(…); int f; Fscanf(pf,”%d”, &f); Int sscanf(const char* str, const char* format, …) Read formatted data from string Char *s; Sscanf(s, “%d”, &f) 7/14
8
Formats 8/14
9
Formats 9/14
10
File Access FILE *fopen (const char* name, char *mode); int fclose(FILE* fp); Example : Ex1 : FILE* fp; fp = fopen(“mytext.txt”, “r”); Ex2 : stdin stdout stderr -Type FILE * -Constant. 10/14
11
File Access SyntaxUseReturn valueExample int getc(FILE* fp);Get a character from stream defined by fp -Success : intvalue of character. -EOF : EOF. -Fail : error indicator is set int a; fp = fopen(“text.txt”, “r”); a = getc(fp); int getchar(void); #define getchar() getc(stdin) Get a character from stdin char c; c = getchar(); char* fgets(char* str, int num, FILE* pf); Get maximum (num - 1) character from stream defined by fp -Success : str -EOF or error : NULL char *s; fgets(s, 5, fp); 11/14
12
File Access SyntaxUseReturn valueExample Int putc (int c, FILE * fp);Write character to stream defined by fp Success : the character is written Fail : EOF and error indicator is set Int a; FILE *pf = fopen(“text.txt”, “w”) Putc(a, pf); Int putchar(int c); int putc(c, stdout); Write character to stdoutInt b; B = putchar(a); Int *fputs(const char* str, FILE *pf); Write string pointed by str to stream defined by pf until reach ‘\0’. Success : non-negative Error : EOF Char *s; Fputs(s, pf); 12/14
13
stderr Why need stderr ? int ferror(FILE *fp); /*check error indicator. If error indicator is set, return non-zero value. Otherwise, return 0.*/ int feof(FILE *fp); /*check EOF indicator. If EOF indicator is set, return non- zero value. If not, return 0.*/ 13/14
14
Thank you ! 14
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.