메뉴 건너뛰기

app

[c] line 수 세는 프로그램

박영식2009.08.14 00:35조회 수 4178댓글 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 조회 4862
박영식
2010.05.25 조회 4206
박영식
2010.01.14 조회 5036
박영식
2009.09.21 조회 4215
박영식
2008.08.18 조회 6133
박영식
2008.08.17 조회 4324
박영식
2008.07.24 조회 4746
박영식
2008.07.23 조회 8091
박영식
2008.07.22 조회 3411
박영식
2008.04.11 조회 2269
박영식
2008.01.20 조회 2105
박영식
2007.12.23 조회 3287
첨부 (0)
위로