2008 3 10 1 mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( ) stream FILE ( man ) 40 ( ) %./a.out String : test Original : test (0x0xbfbd4160) Copy : test (0x0x804a050) %./a.out String : Hello Original : Hello (0x0xbfcb4860) Copy : Hello (0x0x804a050) %./a.out String : Hello World! 1
Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n ) \n ( scanf "%s" 1 ( \n \t ) Hello world! scanf("%s",str) Hello ) stdio.h stdin stdout stderr FILE (fgets 3 ) printf "%p" char *p; : printf("%p\n", p); string.h strlen size_t strlen(const char *s); s size_t int ( man ) malloc void *malloc(size_t size); size ( ) +1 ( \0 ) void * char * 2
mstrcpy malloc ( ) #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STR 40 char *mstrcpy(const char *str); int main(int argc, char *argv[]) { char str[max_str]; char *p; printf("string : "); fgets(&str[0], MAX_STR, stdin); p = mstrcpy(&str[0]); printf("original : %s(0x%p)\n", &str[0], &str[0]); printf("copy : %s(0x%p)\n", p, p); free(p); exit(0); char *mstrcpy(const char *str) { char *p, *q; int len = strlen(str); p = q = (char *)malloc(len + 1); while(*str!= \0 ){ *p++ = *str++; *p = \0 ; return q; 3
2 ( sin, cos, tan, log, exp ) ( ) 2 Please specify a function and a value 1 Undefined function 2 sin cos tan log exp libm % gcc () -lm -lm (libm) (sin cos tan [rad] log exp e(=2.71828... ) ) %./a.out Please specify a function and a value %./a.out cos Please specify a function and a value %./a.out sin 1 2 Please specify a function and a value %./a.out sin 1 Ans : 0.841471 %./a.out cos 1.570796325 Ans : 0.000000 %./a.out tan 2 Ans : -2.185004 %./a.out exp 1 Ans : 2.718282 %./a.out log 2.718282 Ans : 1.000000 %./a.out hoge 10 Undefined function : hoge stdio.h fprintf 4
int fprintf(file *stream, const char *format,...); stream format... printf string.h int strcmp(const char *s1, const char *s2); strcmp s1 s2 0 ( man ) stdlib.h double atof(const char *nptr); atof nptr double ( man ) 5
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main(int argc, char *argv[]) { double v, ans; if(argc!= 3){ fprintf(stderr, "Please specify a function and a value\n"); exit(-1); v = atof(argv[2]); if(strcmp(argv[1], "sin") == 0){ ans = sin(v); else if(strcmp(argv[1], "cos") == 0){ ans = cos(v); else if(strcmp(argv[1], "tan") == 0){ ans = tan(v); else if(strcmp(argv[1], "log") == 0){ ans = log(v); else if(strcmp(argv[1], "exp") == 0){ ans = exp(v); else{ fprintf(stderr, "Undefined function : %s\n",argv[1]); exit(-1); printf("ans : %f\n", ans); exit(0); #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> struct { char *str; double (*func)(double); funcmap[] = { 6
; {"sin", sin, {"cos", cos, {"tan", tan, {"log", log, {"exp", exp, {NULL, NULL int main(int argc, char *argv[]) { double v, ans; int i; if (argc!= 3) { fprintf(stderr, "Please specify a function and a value\n"); v = atof(argv[2]); for(i = 0; funcmap[i].str!= NULL; i++){ if(strcmp(argv[1], funcmap[i].str) == 0){ ans = (*funcmap[i].func)(v); break; if(funcmap[i].str == NULL){ fprintf(stderr, "Undefined function : %s\n", argv[1]); else{ printf("ans : %f\n", ans); exit(0); 7
3 ( A Z ) ( a z ) ( 0 9 ) ( ) ( ) Usage : ( ) <file name> 2 2 Cannot open file : ( ) sample10-3_1.txt sample10-3_2.txt ~ren/pro3_2008_quiz/ %./a.out Usage./a.out <file name>. %./a.out sample10-3_1.txt Upper-case alphabets : 11 Lower-case alphabets : 147 Numbers : 0 Marks : 39 Total : 197 %./a.out sample10-3_2.txt Upper-case alphabets : 76 Lower-case alphabets : 801 Numbers : 0 Marks : 348 Total : 1225 %./a.out hoge.txt Cannot open file : hoge.txt fopen fopen fclose Usage... argv[0] 8
FILE *fopen(const char *path, const char *mode); path mode r w fopen NULL ( man ) int *fclose(file *fp); fp fopen FILE ( man ) int *fgetc(file *fp); fp fopen FILE getchar int ( man ) 9
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int c; int lower, upper, number, mark; int total; FILE *fp; if (argc < 2) { fprintf(stderr, "Usage %s <file name>.\n", argv[0]); if ((fp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Cannot open file : %s\n", argv[1]); total = 0; lower = upper = number = mark = 0; while ((c = fgetc(fp))!= EOF) { total++; if( a <= c && c <= z ){ lower++; else if( A <= c && c <= Z ){ upper++; else if( 0 <= c && c <= 9 ){ number++; else{ mark++; fclose(fp); printf("upper-case alphabets : %d\n", upper); printf("lower-case alphabets : %d\n", lower); printf("numbers : %d\n", number); printf("marks : %d\n", mark); printf("total : %d\n", total); exit(0); 10
4 (0 ) (0 1 ) ( a z ) ( A Z ) 1 1 2 1 2 3 void toupper(file *in, FILE *out); ( ) Cannot open file : ( ) sample10-4_1.txt sample10-4_2.txt sample10-4_3.txt ~ren/pro3_2008_quiz/ % echo test./a.out TEST % echo Hello World!./a.out test.txt % cat test.txt HELLO WORLD! %./a.out sample10-4_1.txt sample10-4_2.txt test2.txt % cat test2.txt PETER PIPER PICKED A PECK OF PICKLED PEPPERS LONDON BRIDGE IS FALLING DOWN, FALLING DOWN, FALLING DOWN. %./a.out sample10-4_3.txt sample10-4_2.txt sample10-4_1.txt test3.txt % cat test3.txt WHO KILLED COCK ROBIN? LONDON BRIDGE IS FALLING DOWN, FALLING DOWN, FALLING DOWN. PETER PIPER PICKED A PECK OF PICKLED PEPPERS %./a.out hoge.txt test4.txt Cannot open file : hoge.txt toupper 11
1 2 stdio.h fputc fputc int fputc(int c, FILE *stream); c (int ) stream fopen FILE 1 stdio.h stdin stdout stderr FILE fputc fgetc stream fopen toupper fgetc fputc (main ) toupper (stdin stdout fopen FILE ) 1 12
#include <stdio.h> #include <stdlib.h> void toupper(file *in, FILE *out); int main(int argc, char *argv[]) { int i; FILE *in, *out; if(argc < 2){ toupper(stdin, stdout); else if(argc == 2){ if((out = fopen(argv[1], "w")) == NULL){ fprintf(stderr, "Cannot open file : %s\n", argv[1]); toupper(stdin, out); fclose(out); else if(argc == 3){ if((in = fopen(argv[1], "r")) == NULL){ fprintf(stderr, "Cannot open file : %s\n", argv[1]); if((out = fopen(argv[2], "w")) == NULL){ fprintf(stderr, "Cannot open file : %s\n", argv[1]); toupper(in, out); fclose(in); fclose(out); else{ if((out = fopen(argv[argc-1], "w")) == NULL){ fprintf(stderr, "Cannot open file : %s\n", argv[1]); for(i = 1; i < argc - 1; i++){ if((in = fopen(argv[i], "r")) == NULL){ fprintf(stderr, "Cannot open file : %s\n", argv[1]); toupper(in, out); fclose(in); 13
fclose(out); exit(0); void toupper(file *in, FILE *out) { int c; while((c = fgetc(in))!= EOF){ if( a <= c && c <= z ){ c = c - a + A ; fputc(c, out); 14
5 ID 1 Yamasaki 170 64 2 Ohmura 165 68 : 1 ID (EOF) (ID ) 40 malloc free next (person ) name struct person_list{ strcut person *next; int id; char *name; int height; int weight; ; Uasage : ( ) <file name> Cannot open file : ( ) 2 2 sample10-5.txt ~ren/pro3_2008_quiz/ %./a.out sample10-5.txt ID : 6 Name : Sato Height : 173 Weight : 66 ID : 5 Name : Imamura Height : 174 Weight : 64 ID : 4 Name : Suito Height : 172 Weight : 60 15
ID : 3 Name : Chishiro Height : 182 Weight : 65 ID : 2 Name : Ohmura Height : 165 Weight : 68 ID : 1 Name : Yamasaki Height : 170 Weight : 62 person list person list malloc malloc free fgets fgetc fscanf fscanf stdio.h int fscanf(file *stream, const char *format,...): stream FILE scanf EOF ( man ) 1 "%s" 1 "%d" "%f" < > < > < > 3 ( ) int a,b,c; : fscanf(fp, "%d%d%d", &a, &b, &c); a,b,c 16
< > < > < > int a,b; char str[40]; : fscanf(fp, "%d%s%d", &a, &str[0], &c); person list name char name 1 mstrcpy ( free mstrcpy malloc ) (person list ) struct person_list *head = NULL; struct person_list *p; : p = (strcut person_list *)malloc(sizeof(struct person_list)); /* p */ p->next = head; head = p; for (p = head; p!= NULL; p = p->next) { /* */ 17
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STR 40 char *mstrcpy(const char *str); struct person_list { struct person_list *next; int id; char *name; int height; int weight; ; struct person_list *list_head = NULL; int main(int argc, char *argv[]) { FILE *fp; int id; char name[max_str]; int height; int weight; struct person_list *p; if(argc < 2){ fprintf(stderr, "Usage : %s <file name>\n", argv[0]); if((fp = fopen(argv[1], "r")) == NULL){ fprintf(stderr, "Cannot open file : %s\n", argv[1]); while(fscanf(fp, "%d%s%d%d\n", &id, &name[0], &height, &weight)!= EOF){ p = (struct person_list *)malloc(sizeof(struct person_list)); if(p == NULL){ fprintf(stderr, "Cannot allocate enough memory\n"); 18
p->id = id; p->name = mstrcpy(&name[0]); p->height = height; p->weight = weight; p->next = list_head; list_head = p; fclose(fp); for(p = list_head; p!= NULL; p = p->next){ printf("id : %d\n", p->id); printf("name : %s\n", p->name); printf("height : %d\n", p->height); printf("weight : %d\n", p->weight); for(p = list_head; p!= NULL; p = p->next){ list_head = p->next; free(p->name); free(p); exit(0); char *mstrcpy(const char *str) { char *p, *q; int len = strlen(str); p = q = (char *)malloc(len + 1); while(*str!= \0 ){ *p++ = *str++; *p = \0 ; return q; 19