160 likes | 419 Views
C programing language. Chapter 7 : Input and Output Present by Le Thi Hien. Contents . Standard Input and Output Variable-length Argument List Formatted Output Formatted Input File Access Error Handling Line Input and Output. Standard Output and Input.
E N D
C programing language Chapter 7 : Input and Output Present by Le ThiHien
Contents • Standard Input and Output • Variable-length Argument List • Formatted Output • Formatted Input • File Access • Error Handling • Line Input and Output
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.
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
Variable-length Argument Lists • Need #include<stdarg.h> • va_listtype • Macro : • va_start(va_listap, lastfix); • va_arg(va_listap, type); • va_end(va_listap); • Syntax : type name (arg1,…); • Example : void minprintf(char *fmt,…) { va_listap; va_start(ap, fmt);/*ap point to 1st unnamed arg*/ … case ‘d’ : ival = va_arg(ap, int); … va_end(ap); }
File Access • FILE *fopen (const char* name, char *mode); • intfclose(FILE* fp); • Example : Ex1 : FILE* fp; fp = fopen(“mytext.txt”, “r”); Ex2 : stdin stdout stderr • Type FILE * • Constant.
stderr • Why need stderr ? • intferror(FILE *fp); /*check error indicator. If error indicator is set, return non-zero value. Otherwise, return 0.*/ • intfeof(FILE *fp); /*check EOF indicator. If EOF indicator is set, return non-zero value. If not, return 0.*/