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

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

I void* School of Information Science, Japan Advanced Institute of Science and Technology

ex14.dvi

I117 7 School of Information Science, Japan Advanced Institute of Science and Technology

I httpd School of Information Science, Japan Advanced Institute of Science and Technology

ohp03.dvi

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

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

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

2008 ( 13 ) C LAPACK 2008 ( 13 )C LAPACK p. 1

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

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

超初心者用

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

char char 1 signed char unsigned char ( ; single-quote 0x27) ASCII Japan Advanced Institute of Science and Technology

r07.dvi

ohp07.dvi

lexex.dvi

PowerPoint プレゼンテーション

(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

r03.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

ohp08.dvi

comment.dvi

debug ( ) 1) ( ) 2) ( ) assert, printf ( ) Japan Advanced Institute of Science and Technology

新版明解C言語 実践編

[ 1] 1 Hello World!! 1 #include <s t d i o. h> 2 3 int main ( ) { 4 5 p r i n t f ( H e l l o World!! \ n ) ; 6 7 return 0 ; 8 } 1:

新・明解C言語 実践編

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裶²ó ¨¡ À©¸æ¹½Â¤¡§·«¤êÊÖ¤· ¨¡

r08.dvi

& & a a * * ptr p int a ; int *a ; int a ; int a int *a

gengo1-12

fl™‹ä1.eps

untitled

gengo1-12

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

memo

I J

memo

double float

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

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

C言語入門

ex12.dvi

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

XMPによる並列化実装2

program.dvi

BW BW

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

C言語によるアルゴリズムとデータ構造

1) // 2) I/O 3) Japan Advanced Institute of Science and Technology 2013/07/26 1

memo

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

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


untitled

slide4.pptx

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

練習&演習問題

273? C

tuat1.dvi

untitled

joho07-1.ppt

プログラミング基礎

PowerPoint プレゼンテーション

AutoTuned-RB

橡Pro PDF

1.ppt

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

計算機プログラミング

DKA ( 1) 1 n i=1 α i c n 1 = 0 ( 1) 2 n i 1 <i 2 α i1 α i2 c n 2 = 0 ( 1) 3 n i 1 <i 2 <i 3 α i1 α i2 α i3 c n 3 = 0. ( 1) n 1 n i 1 <i 2 < <i

j x j j j + 1 l j l j = x j+1 x j, n x n x 1 = n 1 l j j=1 H j j + 1 l j l j E

untitled

演算増幅器

ex01.dvi

‚æ4›ñ

1 ( ) 1.1 (convert.sh) (18GHz 26GHz) C (convert.c, convert1.c) mesure-ryudai convert.sh #!/bin/sh # file1 file1= ls -1 $1 # file1 data for data in $fi

I117 II I117 PROGRAMMING PRACTICE II 2 SOFTWARE DEVELOPMENT ENV. 2 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu

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

PowerPoint プレゼンテーション

卒 業 研 究 報 告.PDF

2) TA Hercules CAA 5 [6], [7] CAA BOSS [8] 2. C II C. ( 1 ) C. ( 2 ). ( 3 ) 100. ( 4 ) () HTML NFS Hercules ( )

02: 変数と標準入出力

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

slide5.pptx

2008 IIA (program) pro(before)+gram(write) (artificial language) (programming languege) (programming) (machine language) (assembly language) ( )

文字数は1~6なので 同じ本数の枝を持つパスで生成される呪文の長さは最大で6 倍の差がある 例えば 上図のようなケースを考える 1サイクル終了した時点では スター節点のところに最強呪文として aaaaaac が求まる しかしながら サイクルを繰り返していくと やがてスター節点のところに aaaaaa

CM-3G 周辺モジュール拡張技術文書 MS5607センサ(温度、気圧)

t 2 2 t 2 t F ( ) p- 2 2 F 2 G F ( ) 2 2 F 2 G F ( ) 2 2 2

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

nakanojoP01

July prog11-02.c /*, */ /* $Id: prog11-02.c,v :48:07+09 naito Exp $ */ #include <stdio.h> #define N 10 int main(int argc, cha

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

C B

untitled

スライド タイトルなし

C言語講座 ~ファイル入出力編~

untitled

Baud Rate 9600 Parity NONE Number of Data Bits 8 Number of Stop Bits 1 Flow Control NONE 1 RS232C 200mm 2,000mm DIMM ( ) Telescope East/West LX200 * 1

Transcription:

I117 9 2 School of Information Science, Japan Advanced Institute of Science and Technology

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(fp);... Japan Advanced Institute of Science and Technology 2008 1-2 1

for(i=0;i<filelistnum;i++) { fp = fopen(flist[i],"r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); Japan Advanced Institute of Science and Technology 2008 1-2 2

argc, argv int main(int argc, char *argv[]) {... argc argv argv[0] Japan Advanced Institute of Science and Technology 2008 1-2 3

argc, argv (cont.) #include <stdio.h> int main(int argc, char *argv[]) { int i; for(i=0;i<argc;i++) printf("%d %s\n", i, argv[i]); %./a.out A B 0./a.out 1 A 2 B Japan Advanced Institute of Science and Technology 2008 1-2 4

argc, argv (cont.)... argv[0] #include <stdio.h> int main(int argc, char *argv[]) { char line[bufsiz]; int i; FILE *fp; for(i=1;i<argc;i++) { fp = fopen(argv[i],"r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); Japan Advanced Institute of Science and Technology 2008 1-2 5

#include <stdio.h> int main(int argc, char *argv[]) { char line[bufsiz]; FILE *fp; int i, lc=0; for(i=1;i<argc;i++) { fp = fopen(argv[i],"r"); while(fgets(line, BUFSIZ, fp)) { lc++; fclose(fp); printf("%d\n",lc); Japan Advanced Institute of Science and Technology 2008 1-2 6

(cont.) wc %./a.out xargcv.c nlc01.c 21 % wc xargcv.c nlc01.c 8 18 138 xargcv.c 13 30 243 nlc01.c 21 48 381 total Japan Advanced Institute of Science and Technology 2008 1-2 7

(cont.) #include <stdio.h> int linecount(char *fname) { char line[bufsiz]; FILE *fp; int lc=0; fp = fopen(fname,"r"); while(fgets(line, BUFSIZ, fp)) { lc++; fclose(fp); return lc; Japan Advanced Institute of Science and Technology 2008 1-2 8

(cont.) main linecount int main(int argc, char *argv[]) { int i, lc, sum=0; for(i=1;i<argc;i++) { lc = linecount(argv[i]); sum += lc; printf("%d\n",sum); Japan Advanced Institute of Science and Technology 2008 1-2 9

(cont.) %./a.out *.c 40 % wc *.c 12 30 245 nlc01.c 20 46 357 nlc01b.c 8 18 138 xargcv.c 40 94 740 total Japan Advanced Institute of Science and Technology 2008 1-2 10

fopen fp = fopen(fname,"r"); if(fp==null) return -1; linecount lc = linecount(argv[i]); if(lc<0) continue; Japan Advanced Institute of Science and Technology 2008 1-2 11

(cont.) fp = fopen(fname,"r"); if(fp==null) { fprintf(stderr, "can not open file %s\n", fname); return -1; %./a.out ignorefilename *.c can not open file ignorefilename 109 Japan Advanced Institute of Science and Technology 2008 1-2 12

(cont.) wc % wc ignorefilename *.c wc: ignorefilename: No such file or direct 12 30 245 nlc01.c 20 46 357 nlc01b.c 34 55 455 nlc01c.c 35 62 507 nlc01d.c 8 18 138 xargcv.c 109 211 1702 total Japan Advanced Institute of Science and Technology 2008 1-2 13

typedef struct { char *fname; FILE *fp; char line[bufsiz]; int lc; int reach_eof; lcparam; file"a" file"b" file"c" file"d" file"e" reach eof Japan Advanced Institute of Science and Technology 2008 1-2 14

(cont.) open read close Japan Advanced Institute of Science and Technology 2008 1-2 15

(cont.) lcparam *lclist; lclist = (lcparam*)malloc( sizeof(lcparam)*(argc)); for(i=1;i<argc;i++) { lc = openfile(&lclist[i], argv[i]); if(lc<0) { continue; Japan Advanced Institute of Science and Technology 2008 1-2 16

do { /* process reading */ for(i=1;i<argc;i++) { lc = linecount(&lclist[i]); /* check all file was read */ e = 0; for(i=1;i<argc;i++) { if(lclist[i].reach_eof>0) { e++; while(e<argc-1);

(cont.) sum = 0; for(i=1;i<argc;i++) { printf("%s %d\n", lclist[i].fname, lclist[i].lc); sum += lclist[i].lc; if(lclist[i].fp) { fclose(lclist[i].fp); printf("total %d\n",sum); Japan Advanced Institute of Science and Technology 2008 1-2 18

(cont.) int openfile(lcparam *lcp, char *fname) { FILE *fp; lcp->reach_eof = -1; lcp->fp = NULL; lcp->lc = 0; lcp->fname = strdup(fname); fp = fopen(fname,"r"); if(fp==null) { fprintf(stderr, "can not open file %s\n", fname); Japan Advanced Institute of Science and Technology 2008 1-2 19

(cont.) lcp->reach_eof = 1; return -1; lcp->fp = fp; lcp->reach_eof = 0; return 0; -1 0 Japan Advanced Institute of Science and Technology 2008 1-2 20

(cont.) int linecount(lcparam *lcp) { if(lcp->reach_eof!=0) { return -1; if(fgets(lcp->line,bufsiz,lcp->fp)){ lcp->lc++; return 0; lcp->reach_eof = 1; return 1; Japan Advanced Institute of Science and Technology 2008 1-2 21

(cont.) %./a.out *.c nlc01.c 12 nlc01b.c 20 nlc01c.c 34 nlc01d.c 35 plc01.c 90 plc01b.c 87 xargcv.c 8 total 286 Japan Advanced Institute of Science and Technology 2008 1-2 22

1) (BUFSIZ ) 2) 3) wc Japan Advanced Institute of Science and Technology 2008 1-2 23

(cont.) 4) close open read close Japan Advanced Institute of Science and Technology 2008 1-2 24