Q 8 1
8.1 (C++) C++ cin cout 5
C++ 16 6
p.63 8.3 #include <iomanip> 7
showbase noshowbase showpoint noshowpoint 8.3 uppercase 16 nouppercase 16 setfill(int) setprecision(int) setw(int) setbase(int) dec 10 hex 16 oct left right fixed scientific flush ends endl unitbuf nounitbuf 8 10 skipws noskipws #include <iomanip> ws 8
001 #include <iostream> 002 #include <iomanip> 23 iostream iomanip 003 int main ( void ) { (<<) 004 std:: cout << std:: hex << std:: setw(8) 005 << std:: setfill ( # ) << 1234 << std:: endl; 006 return 0; 007 } 9
eof() EOF(end EOF( d of file) true fail() I/O true bad() true good() true true clear() if (cin.fail()) 10
8.2 (C++) #include <fstream> 3 ifstream fin; ofstream fout; fstream fboth; ifstream ofstream fstream 11
001 002 003 24 #include <fstream> #include <string> int main (void) { std :: ifstream infile; std :: ofstream outfile; infile.open( file1 ); outfile.open( file2 ); std :: string s; while( getline(infile,s)){ outfile << s << std :: endl; } infile.close( (); outfile.close( ); return 0; } fstream 004 005 006 007 008 009 010 011 012 013 014 015 string s 24 11 12
in out app ate trunc binary 13
25 001 #include <fstream> fstream 002 003 004 int main (void){ std :: ifstream file; file.open( open( file1 ); 005 006 // file1 file.close( ); file.clear( ); 008 file.open( file2 ); 009 //file2 010 file.close( ); 011 return 0; 012 } 2 007 14
8.3 string (C++) #include <sstream> istringstream ostringstream stringstream string istream string ostream string iostream istringstream ostringstream stringstream string 15
8.4 (C) printf printf( i,... ); % printf("%d %f %c", 2*10, 10.0/5.0, '1'); 20 2.000000 1 printf("%+5 %+5.2f 2f", 10.0/5.0); 0/5 0); +2.00 printf("%a", 1.0/10); printf("system %s Engineering", "Design"); 0x1.999999999999ap 4 System DesignEngineering 16
8.4 (C) scanf scanf(,... ); float 20 2.000000 1 scanf("%d %f %c", &i, &f, &c); +2.00 200 scanf("%+5.2lf", &d); 0x1.999999999999ap 4 scanf("%la", &d); System Design Engineering scanf("system %s Engineering", s); 17
8.4 (C) puts, fputs puts( ); fputs(, ); puts("hello n"); fputs("hello n", stdout); fgets, fgets gets( ); fgets(,, ); char s[100]; gets(s); fgets(s, 100, stdin); 18
8.5 (C) FILE <stdio.h> h FILE fp ; 1. 2. 3. 19
FILE *fopen(char *filename, char *mode) w: a: r: r+, w+, a+ : rb, wb, ab: fp=fopen( data.txt, r ); int fclose(file *fp) i= fclose(fp); int fprintf(file *fp,char *format,..... ) fprintf(fp, Hello %d n,i); int fscanf(file *fp,char *format,..... ) fscanf(fp, %d,&i);
26 001 #include <stdio.h> 002 int main(void) { 003 int age; 004 float height; 005 char name[40]; 006 FILE *fp; 007 if ((fp = fopen("personal.txt","w")) == NULL){ 008 fprintf(stderr, File open failed. n"); 009 return 1; 010 } 011 while(1){ 012 printf( Input age height name: n"); 013 if (scanf("%d %f %s",&age,&height,name)<3) 014 break; 015 fprintf(fp,"%d %f %s n",age,height,name); 016 } 017 fclose(fp); 018 return (0); 019 }
26 001 #include <stdio.h> 002 int main(void) { 003 int age; 004 float height; 005 char name[40]; 006 FILE *fp; 007 if ((fp = fopen("personal.txt","w")) == NULL){ 008 fprintf(stderr, File open failed. n"); 009 return 1; 010 } 011 while(1){ 012 printf( Input age height name: n"); 013 if (scanf("%d %f %s",&age,&height,name)<3) 014 break; 015 fprintf(fp,"%d %f %s n",age,height,name); 016 } 017 fclose(fp); 018 return (0); 019 }
26 001 002 #include <stdio.h> int main(void) { stdio.h 003 005 int age; char name[40]; 004 006 float height; FILE *fp; 007 if ((fp = fopen("personal.txt","w")) == NULL){ 008 fprintf(stderr, File open failed. n"); 009 return 1; 010 } 011 while(1){ 012 printf( Input age height name: n"); 013 if (scanf("%d %f %s",&age,&height,name)<3) 014 break; 015 fprintf(fp,"%d %f %s n",age,height,name); 016 } fclose(fp); return (0); 017 018 019 }
8.6 int sprintf(char *sp, char *format,...) sprintf(sp, "Hello %d n", i); int sscanf(char *sp, char *format,...) sscanf(sp, "%d", &i); 24
8.1 8.7 1. 2. 25