메뉴 건너뛰기

app

[c] line 수 세는 프로그램

박영식2009.08.14 00:35조회 수 4225댓글 0

    • 글자 크기
#include <fcntl.h>
#include <stdio.h>

long ch;
long word;
long line;

main(void)
{
FILE *ifp, *ofp;
char buffer[BUFSIZ];
int file, j, i;

ifp = fopen("input.txt", "r");
ofp = fopen("output.txt", "w");

if((file = open("input.txt", O_RDONLY)) < 0) {
perror("open error");
exit(1);
}
while( (j = read(file, buffer, BUFSIZ )) > 0 ) {
ch += j;
for(i=0 ; i < j ; i++) {
if( buffer[i]==' ' || buffer[i]=='n') {
word++;
if(buffer[i] == 'n')
line++;
}
}
}
fprintf(ofp, "글자수 : %ldn워드수 : %dn라인수 : %dn", ch, word, line);
fclose(ifp);
fclose(ofp);
}


이를 linux에서 gcc를 이용해 컴파일 할 수 있다.

# gcc -o line line.c
#./line
으로 실행할 수 있다.
박영식 (비회원)
    • 글자 크기
[PHP] letter, word, line 수 세기 (by 박영식) [c] 소수 여부 판별하는 프로그램 (by 박영식)

댓글 달기

박영식
2010.09.09 조회 4901
박영식
2010.05.25 조회 4248
박영식
2010.01.14 조회 5086
박영식
2009.09.21 조회 4267
박영식
2008.08.18 조회 6186
박영식
2008.08.17 조회 4375
박영식
2008.07.24 조회 4790
박영식
2008.07.23 조회 8139
박영식
2008.07.22 조회 3463
박영식
2008.04.11 조회 2316
박영식
2008.01.20 조회 2161
박영식
2007.12.23 조회 3330
첨부 (0)
위로