c언어 파일입출력인데요..
나머지 다 각설하고,, 맨 밑에 메인함수에서 파일을 못 읽어요ㅠ
왜 그런거죠???ㅜㅜㅜ
#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;
}
errno로 확인
fp=fopen("input.txt","r");//파일 오픈
fo=fopen("output.txt","w");
이부분에서
if (fp == NULL)
{
printf("Error = %s\n", strerror(errno));
}
이렇게 하셔서 errno를 출력해 보시면 정확히 무엇때문에 오픈이 안되는지 알수 있을꺼 같습니다.
댓글 달기