제가다시 작성한 c프로그램인데요,, 머가 잘못된건지 잘모르겠어

nidle의 이미지

파일에있는 문자의 단어의수를 세는 프로그램을 만들었습니다
개별적으로 하면 함수는 다컴파일되더라구요,, 근데 이렇게 합쳐서 하면.. file open error이나와
요, 왜그런건지.알려주세요
그리구 저는 볼랜드c 3.2윈도우용을 쓰고 있습니다..
같은걸 다시올려서 죄송합니다..EOF인가 때문이라구 하시는데..
이해가 잘안되서요,, 어떻게 고치면 되는건지 알려주세요

#include <stdio.h>
#include <stdlib.h>

/*알파벳 확인 함수*/

int alp(char c)
{
if ((c>='a' && c<='z') || (c>='A' && c <= 'Z'))
return(1);
else
return(0);
}
/*한줄읽기함수*/

readline1(char buffer[])
{
char ch;
int i=0;
FILE *aa;
aa = fopen("a.txt","r");
if( aa == NULL )
{
printf("file open error \n");
exit(1);
}

do {
ch=getc(aa);
buffer1[i]=ch;
++i;
}
while(ch != '\n');

buffer[i -1] = '\0';

}

/*단어의수 센다 함수*/

int count(char string[])

{
int i,looking =1,wordc =0;

for(i=0;string[i] !='\0'; ++i)
if(alp(string[i])){
if(looking){
++wordc;
looking=0;
}
}else
looking=1;

return(wordc);
}

main()
{

char text[81];
int end=0, total =0;

while ( ! end) {
readline1(text);

if (text[0] == '\0')
end=1;
else
total+=count(text);

}
printf("%d",total);

}

a.txt: apple
pineapple
strawberry
tomato
orange
watermelon
melon
mango
cherry
grapes

moonhyunjin의 이미지

#include <stdio.h> 
#include <stdlib.h> 

/*알파벳 확인 함수*/ 

int alp(char c) 
{ 
if ((c>='a' && c<='z') || (c>='A' && c <= 'Z')) 
return(1); 
else 
return(0); 
} 
/*한줄읽기함수*/ 

readline1(char buffer[]) 
{ 
char ch; 
int i=0; 
FILE *aa; 
aa = fopen("a.txt","r"); 
if( aa == NULL ) 
{ 
printf("file open error \n"); 
exit(1); 
} 


do { 
ch=getc(aa); 
buffer1[i]=ch; 
++i; 
} 
while(ch != '\n'); 


buffer[i -1] = '\0'; 

} 


/*단어의수 센다 함수*/ 

int count(char string[]) 

{ 
int i,looking =1,wordc =0; 

for(i=0;string[i] !='\0'; ++i) 
if(alp(string[i])){ 
if(looking){ 
++wordc; 
looking=0; 
} 
}else 
looking=1; 

return(wordc); 
} 


main() 
{ 

char text[81]; 
int end=0, total =0; 

while ( ! end) { 
readline1(text); 

if (text[0] == '\0') 
end=1; 
else 
total+=count(text); 

} 
printf("%d",total); 



} 

<- 이거면 안 되는 게 없어~
정품 소프트웨어 사용 캠패인

new5244의 이미지

readline1(char buffer[]) 
{ 
char ch; 
int i=0; 
FILE *aa; 
aa = fopen("a.txt","r"); 
if( aa == NULL ) 
{ 
printf("file open error \n"); 
exit(1); 
} 


do { 
ch=getc(aa); 
buffer1[i]=ch; 
++i; 
} 
while(ch != '\n'); 


buffer[i -1] = '\0'; 

} 

위 함수는 a.txt 라는 파일을 연후 제일 첫 라인을 읽어서 buffer 에 저장 시킨후 끝나내요. 사용한 FILE * 를 닫지 않네요.

main() 
{ 

char text[81]; 
int end=0, total =0; 

while ( ! end) { 
readline1(text); 

if (text[0] == '\0') 
end=1; 
else 
total+=count(text); 

} 
printf("%d",total); 



} 

메인 함수는 readline1() 함수를 text[0] == 0 이 될때까지 계속 호출하네요.
위 readline1() 함수를 봤을때 위 코드는 계속해서 무한 loop 를 돌게 되 있습니다(a.txt 의 첫 줄만 계속 읽는거죠). 그런데 readline1() 에서 파일을 열고서 close() 를 하지 않았으므로 결국엔 동시에 open 할 수 있는 파일 개수가 제한되므로 더이상 파일을 열수가 없어 에러가 발생하는것 같군요

from saibi

nidle의 이미지

fclose()를 하면 터보c가 멈춰버립니다..오류라면서,, 강제종료해야됩니다

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.