Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

Similar documents
mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

卒 業 研 究 報 告.PDF

ohp08.dvi

r08.dvi

: CR (0x0d) LF (0x0a) line separator CR Mac LF UNIX CR+LF MS-DOS WINDOWS Japan Advanced Institute of Science and Technology

超初心者用

program.dvi

ex14.dvi

r07.dvi

新・明解C言語 ポインタ完全攻略

ohp07.dvi

(K&R 2.9) ~, &,, >>, << 2. (K&R 5.7) 3. (K&R 5.9) 4. (K&R 5.10) (argc argv atoi(), atof() ) 5. (K&R 7.5) (K&R 7.6) - FILE, stdin, stdout, std

スライド タイトルなし

PowerPoint プレゼンテーション

新版明解C言語 実践編

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

新・明解C言語 実践編

slide4.pptx

1. ファイルにアクセスするには ファイルにアクセスするには 1. ファイルを開く 2. アクセスする 3. ファイルを閉じるという手順を踏まなければなりません 1.1. ファイルを読み込む まずはファイルの内容を画面に表示させるプログラムを作りましょう 開始 FILE *fp char fname

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

ファイル入出力

memo

ファイル入出力

ohp03.dvi

untitled

untitled

Microsoft Word - Cプログラミング演習(12)

1.ppt

r03.dvi

file"a" file"b" fp = fopen("a", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); fp = fopen("b", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裵²ó ¨¡ À©¸æ¹½Â¤¡§¾ò·ïʬ´ô ¨¡

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

lexex.dvi

02: 変数と標準入出力

C

Microsoft PowerPoint - kougi9.ppt

joho07-1.ppt

Microsoft Word - C.....u.K...doc

memo

プログラミング基礎

Microsoft PowerPoint - CproNt11.ppt [互換モード]

gengo1-12

comment.dvi

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

目次

PowerPoint プレゼンテーション

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

計算機プログラミング

Microsoft Word - Cプログラミング演習(9)

02: 変数と標準入出力

プログラミング及び演習 第1回 講義概容・実行制御

gengo1-12

Microsoft Word - 06

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

C言語入門

橡Pro PDF

情報処理演習 B8クラス

untitled

プログラミング演習3 - Cプログラミング -

+ +

£Ã¥×¥í¥°¥é¥ß¥ó¥°(2018) - Âè11²ó – ½ÉÂꣲ¤Î²òÀ⡤±é½¬£² –

9.4 #define for while

プログラミング及び演習 第1回 講義概容・実行制御

Cプログラミング1(再) 第2回

PowerPoint Presentation

:30 12:00 I. I VI II. III. IV. a d V. VI

tuat1.dvi

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

slide5.pptx

Microsoft Word - no15.docx

double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.t

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

PowerPoint Presentation

18 C ( ) hello world.c 1 #include <stdio.h> 2 3 main() 4 { 5 printf("hello World\n"); 6 } [ ] [ ] #include <stdio.h> % cc hello_world.c %./a.o

Taro-ファイル処理(公開版).jtd

第7章 有限要素法のプログラミング

tuat2.dvi

:30 12:00 I. I VI II. III. IV. a d V. VI

memo

double float

1 $ cat aboutipa 2 IPA is a Japanese quasi-government 3 organization established in accor- 4 dance with The Law for Information 5 Processing Technolog

ディジタル信号処理

untitled

演算増幅器

gengo1-12

PowerPoint プレゼンテーション

Taro-リストⅢ(公開版).jtd

3.1 stdio.h iostream List.2 using namespace std C printf ( ) %d %f %s %d C++ cout cout List.2 Hello World! cout << float a = 1.2f; int b = 3; cout <<

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裱£²²ó ¡Ý½ÉÂꣲ¤Î²òÀ⡤±é½¬£²¡Ý

Microsoft Word - cpro_m_13.doc

[1] #include<stdio.h> main() { printf("hello, world."); return 0; } (G1) int long int float ± ±


問 2 ( 型変換 ) 次のプログラムを実行しても正しい結果が得られない 何が間違いかを指摘し 正しく修正せよ ただし int サイズが 2 バイト long サイズが 4 バイトの処理系での演算を仮定する #include <stdio.h> int main( void ) { int a =

プログラミング方法論 II 第 14,15 回 ( 担当 : 鈴木伸夫 ) 問題 17. x 座標と y 座標をメンバに持つ構造体 Point を作成せよ 但し座標 は double 型とする typedef struct{ (a) x; (b) y; } Point; 問題 18. 問題 17 の

slide6.pptx

O(N) ( ) log 2 N

Transcription:

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