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

Similar documents
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

超初心者用

ex14.dvi

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

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

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

program.dvi

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

untitled

新・明解C言語 実践編

新版明解C言語 実践編

(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

r07.dvi

ohp07.dvi

(2 Linux Mozilla [ ] [ ] [ ] [ ] URL 2 qkc, nkc ~/.cshrc (emacs 2 set path=($path /usr/meiji/pub/linux/bin tcsh b

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

double float

ファイル入出力

comment.dvi

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU

6 6.1 sound_wav_files flu00.wav.wav 44.1 khz 1/44100 spwave Text with Time spwave t T = N t N 44.1 khz t = 1 sec j t f j {f 0, f 1, f 2,, f N 1

Apache Web Server 2 Compaq ActiveAnswers Deskpro Compaq Insight Manager Fastart Systempro Systempro/LT ProLiant ROMPaq Qvision SmartStart NetFlex Quic

p = 1, 2, cos 2n + p)πj = cos 2nπj 2n + p)πj, sin = sin 2nπj 7.1) f j = a ) 0 + a p + a n+p cos 2nπj p=1 p=0 1 + ) b n+p p=0 sin 2nπj 1 2 a 0 +

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

橡Pro PDF

untitled

1.ppt

新版明解C言語入門編

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

情報処理演習 B8クラス

Microsoft PowerPoint - kougi2.ppt

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

r08.dvi

02: 変数と標準入出力

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

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

/* 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

BW BW

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

ohp03.dvi

273? C

‚æ2›ñ C„¾„ê‡Ìš|

tuat2.dvi

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) * *

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

[ 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:

PowerPoint プレゼンテーション

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

C 2 / 21 1 y = x 1.1 lagrange.c 1 / Laglange / 2 #include <stdio.h> 3 #include <math.h> 4 int main() 5 { 6 float x[10], y[10]; 7 float xx, pn, p; 8 in

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

P05.ppt

r03.dvi

C

Transcription:

I117 8 1 School of Information Science, Japan Advanced Institute of Science and Technology

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

od % ls -1 head -2 I117-0.aux I117-0.dvi % ls head -2 od -c 0000000 I 1 1 7-0. a u x \n I 1 1 0000020 0. d v i \n 0000026 % ls head -2 od -t x1 0000000 49 31 31 37 2d 30 2e 61 75 78 0a 49 31 31 37 2d 0000020 30 2e 64 76 69 0a 0000026 LF (\n 0x0a) Japan Advanced Institute of Science and Technology 2008 1-2 2

C OS fopen fgets fputs, fprintf fclose Japan Advanced Institute of Science and Technology 2008 1-2 3

FILE *fp; char line[bufsiz]; fp = fopen("foo", "r"); if(!fp) { } while(fgets(line, BUFSIZ, fp)) { } fclose(fp); Japan Advanced Institute of Science and Technology 2008 1-2 4

fp = fopen("foo", "w"); if(!fp) { } while() { fprintf(fp, "%s\n", line); fputs(line, fp); } fclose(fp); Japan Advanced Institute of Science and Technology 2008 1-2 5

stdin stdout stderr stdin program stdout stderr Japan Advanced Institute of Science and Technology 2008 1-2 6

(cont.) a.out lc.c %./a.out < lc.c 12 lines Japan Advanced Institute of Science and Technology 2008 1-2 7

(cont.) ls a.out % ls./a.out a.out wc %./a.out wc 1 2 9 3 Japan Advanced Institute of Science and Technology 2008 1-2 8

: BUFSIZ... 512, 1024, 4096, 8192 setvbuf Japan Advanced Institute of Science and Technology 2008 1-2 9

: BUFSIZ (cont.) BUFSIZ #include <stdio.h> int main() { printf("bufsiz %d\n", BUFSIZ); } SunOS 5.9 1024 %./a.out BUFSIZ 1024 Japan Advanced Institute of Science and Technology 2008 1-2 10

#include <stdio.h> #include <stdlib.h> int main() { int lno; char line[bufsiz]; lno = 0; while(fgets(line, BUFSIZ, stdin)) lno++; printf("%d lines\n", lno); } Japan Advanced Institute of Science and Technology 2008 1-2 11

(cont.) : 5 % ls / head -5./a.out 5 lines fgets BUFSIZ wc Japan Advanced Institute of Science and Technology 2008 1-2 12

(cont.) /tmp/z wc % echo man man > /tmp/z % echo man man >> /tmp/z % echo man man >> /tmp/z % cat /tmp/z wc 3 5724 38100 % cat /tmp/z./a.out 39 lines Japan Advanced Institute of Science and Technology 2008 1-2 13

n<bufsiz LF LF BUFSIZ n>=bufsiz LF LF n>>bufsiz LF

(cont.) line wc while(fgets(line, BUFSIZ, stdin)) { lw = strlen(line); if(lw>=1 && line[lw-1]== \n ) { lno++; } } % cat /tmp/z./a.out 3 lines Japan Advanced Institute of Science and Technology 2008 1-2 15

#include <stdio.h> #include <stdlib.h> #include <string.h> int Xstrcmp(const void *x, const void *y) { char *xp=*(char**)x, *yp=*(char**)y; return strcmp(xp, yp); } Japan Advanced Institute of Science and Technology 2008 1-2 16

#define NLINES (100) char tmp[bufsiz]; char **lines; int n, i; n = 0; lines = (char**)malloc(sizeof(char*)*nlines); while(fgets(tmp, BUFSIZ, stdin)) { if(n>nlines-1) { break; } lines[n] = strdup(tmp); n++; } qsort(lines, n, sizeof(char*), Xstrcmp);

(cont.) input % cat in.lsort jaist asahida tatukuchi ishikawa japan output %./a.out < in.lsort 0: asahida 1: ishikawa 2: jaist 3: japan 4: tatukuchi Japan Advanced Institute of Science and Technology 2008 1-2 18

331.0 43.10 332.5 39.41 380.6 48.18 0-9 (0x2e)> (0x20) (\t, 0x09) Japan Advanced Institute of Science and Technology 2008 1-2 19

(cont.) white(s) real number real number 3 3 1. 0 \t 4 3. 1 0 \n while (< >) { ( ) } Japan Advanced Institute of Science and Technology 2008 1-2 20

(char*p) #define takereal(r) { \ char tmp[bufsiz]; char *q = tmp;\ r = -1.0; \ while((*p>= 0 &&*p<= 9 ) *p==. ){\ *q++ = *p++; } \ *q = \0 ; \ if(*tmp) r = atof(tmp); \ while(*p&&(*p== *p== \t )) p++; } -1 Japan Advanced Institute of Science and Technology 2008 1-2 21

Japan Advanced Institute of Science and Technology 2008 1-2 22

; char line[bufsiz]; char *p; double a1, a2; while(fgets(line, BUFSIZ, stdin)) { p = line; takereal(a1); takereal(a2); printf("%.2f\n", a1/a2); } Japan Advanced Institute of Science and Technology 2008 1-2 23

; (cont.) %./a.out < G1 7.68 8.44 7.90 7.90 [km/l] Japan Advanced Institute of Science and Technology 2008 1-2 24

; double a1, sum; sum = 0.0; while(fgets(line, BUFSIZ, stdin)) { p = line; takereal(a1); sum += a1; } printf("%.2f\n", sum); Japan Advanced Institute of Science and Technology 2008 1-2 25

; double sum = 0.0; int num=0; while(fgets(line, BUFSIZ, stdin)) { p = line; takereal(a1); sum += a1; num++; } printf("%.2f %d ave %.2f\n", sum, num, sum/num); Japan Advanced Institute of Science and Technology 2008 1-2 26

; %./a.out < G1 1044.10 %./a.out < G1 1044.10 3 ave 348.03 Japan Advanced Institute of Science and Technology 2008 1-2 27

# (0x23) p = line; if(*p== # ) { continue; } if(*p== \n ) { continue; } Japan Advanced Institute of Science and Technology 2008 1-2 28

#define takereal(r) { \ char tmp[bufsiz]; char *q = tmp;\ r = -1.0; \ while(*p&&(*p== *p== \t )) p++; \ while((*p>= 0 &&*p<= 9 ) *p==. ) {\ *q++ = *p++; } \ *q = \0 ; \ if(*tmp) r = atof(tmp); } Japan Advanced Institute of Science and Technology 2008 1-2 29

1) (BUFSIZE ) 2) 3) Japan Advanced Institute of Science and Technology 2008 1-2 30

(cont.) 4) 35.59 9.09 0.29 105.78 55.86 76.18 9.62 112.53 5.35 43.43 5) 4) Japan Advanced Institute of Science and Technology 2008 1-2 31

(cont.) 6) int nthreal(double *dst, char *src, int n); Japan Advanced Institute of Science and Technology 2008 1-2 32

(cont.) 7) a) x i b) f i f k n c) k i=1 f i d) k i=1 f i n e) 5 x k, f k, f k n, k i=1 f i, k i=1 f i n Japan Advanced Institute of Science and Technology 2008 1-2 33

w/o dup 11 12 13 14 15 16 17 100 11 1 14.29 1 14.29 12 1 14.29 2 28.57 80 13 1 14.29 3 42.86 60 14 1 14.29 4 57.14 40 15 1 14.29 5 71.43 20 16 1 14.29 6 85.71 0 17 1 14.29 7 100.00 CDF [%] 11 12 13 14 15 16 17 w/ dup 11 14 13 11 14 16 14 11 2 28.57 2 28.57 40 13 1 14.29 3 42.86 20 14 3 42.86 6 85.71 0 16 1 14.29 7 100.00 CDF [%] 100 80 60 x i 11 12 13 14 15 16 x i