c언어 파일입출력인데요..

gomu2486의 이미지

나머지 다 각설하고,, 맨 밑에 메인함수에서 파일을 못 읽어요ㅠ
왜 그런거죠???ㅜㅜㅜ

#include
#include //srand(), rand()함수가 포함된 라이브러리
#include
#include //_beginthread()함수가 포함되어 있는 라이브러리

//버퍼를 나타내는 구조체
typedef struct _bupper
{
int b_num;//버퍼의 번호.(몇번 버퍼인지..)
int flag;//버퍼의 flag가 꽉 찼는지(1이면 꽉 차 있거나 비우는 중,
// 0이면 비어있거나 채우는 중)
int count;//저장할 블로킹을 가르키고 있는 변수(index개념) = record counter
int value[10];//버퍼 구현을 위해 저장해 둘 공간
struct _bupper * next; //이중버퍼는 서로의 주소를 가르키므로
// 주소를 가르킬 포인터 필요.
} bupper;

FILE *fp, *fo;

bupper b1, b2;//버퍼1 과 버퍼2를 생성
bupper * to_fill, *to_empty;//to_fill은 채워지고 있는 버퍼를 가르킴
//to_empty는 비워지고 있는 버퍼를 가르킴
int Max_data;
int input_num=0;
int output_num=0;

void init_bupper(bupper *b1, bupper *b2)
{

b1->b_num=1;
b2->b_num=2;//버퍼 넘버.

b1->flag=b2->flag=0;//버퍼의 flag를 0으로 초기화
b1->count=b2->count=0;//버퍼의 카운터를 0으로 초기화

b1->next = b2;
b2->next = b1;//다음 버퍼의 주소를 가지고 있음

to_fill = b1;
to_empty = b1;//처음에 to_fill & to_empty를 b1로 셋팅
}

unsigned __stdcall product(void *arg)//스레드 함수
{
while(!(Max_data == input_num))
{
if(to_fill->flag==1)
continue;

else
{
fscanf(fp, "%d", &to_fill->value[to_fill->count]);
fprintf(fo,"[put] value : %d bupper : %d\n", to_fill->value[to_fill->count],
to_fill->b_num);
to_fill->count=(to_fill->count+1)%10;//record_counter는 순환하기때문에//

if(to_fill->count == 0)
{
to_fill->flag=1;
to_fill=to_fill->next;
}

fprintf(fo,"\tbuffer num: 1 full flag: %s record counter: %d\n",
b1.flag==1 ? "true-" : "false", b1.count);
fprintf(fo,"\tbuffer num: 2 full flag: %s record counter: %d\n",
b2.flag==1 ? "true-" : "false", b2.count);

input_num++;

Sleep(100);
}
}
return 0;
}

unsigned __stdcall consume(void *arg)//스레드 함수
{
while(!(Max_data == output_num))
{
if(to_empty->flag==0)
continue;

else
{
fprintf(fo,"[get] value : %d bupper : %d\n", to_empty->value[to_empty->count],
to_empty->b_num);
to_empty->count = (to_empty->count+1)%10;//record_counter는 순환하기때문에//
if(to_empty->count ==0)
{
to_empty->flag=0;
to_empty=to_empty->next;
}

fprintf(fo,"\tbuffer num: 1 full flag: %s record counter: %d\n",
b1.flag==1 ? "true-" : "false", b1.count);
fprintf(fo,"\tbuffer num: 2 full flag: %s record counter: %d\n",
b2.flag==1 ? "true-" : "false", b2.count);

output_num++;

Sleep(100);
}
}
return 0;
}

int main()
{
init_bupper(&b1, &b2);//버퍼 초기화

fp=fopen("input.txt","r");//파일 오픈
fo=fopen("output.txt","w");

fscanf(fp,"%d", &Max_data);//데이터의 개수를 입력 받음.

_beginthreadex(NULL, 0, product, 0, 0,NULL);//생산자 스레드 시작
_beginthreadex(NULL, 0, consume, 0, 0,NULL);//소비자 스레드 시작

while(1)//파일 입출력에서 input.txt에서 제시한 파일의 개수와
{ //output.txt에 기록한 파일의 개수가 같다면 프로그램을 종료!
if(Max_data == output_num)
break;
}

fclose(fp);
fclose(fo);

return 0;
}

nouveou의 이미지


fp=fopen("input.txt","r");//파일 오픈
fo=fopen("output.txt","w");

이부분에서

if (fp == NULL)
{
printf("Error = %s\n", strerror(errno));
}
이렇게 하셔서 errno를 출력해 보시면 정확히 무엇때문에 오픈이 안되는지 알수 있을꺼 같습니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.