write로 데이터 쓴 파일을 read 하면 잘 나오다가 어느 순간부터 NULL로 찍힙니다.
글쓴이: willisone / 작성시간: 토, 2016/10/15 - 5:13오후
파일을 생성해서 데이터를 쓸 때 모두 잘 들어갔습니다. 배열 하나하나 hex값 다 찍어봤구요, 그런데 read 함수로 실행시켜 보면 2줄 밖에 읽어들이지 못하고 나머지는 모두 NULL값이 찍힙니다.... 도대체 무슨 문제인건가요??? 아무리 찾아봐도 잘 모르겠습니다.
그런데 직접 vi에디터로 파일을 생성해서 그 파일안에 데이터를 넣어놓고 파일 read코드 작성해놓은 걸로 실행해보면 모두 잘 읽힙니다...
아마 write쪽 코드에서 문제가 생긴 것 같은데 뭐가 문제일까요? 도와주세요
//파일위치와 생성할 파일명을 쓰고 해당 파일 디스크립터에 데이터를 write하는 코드입니다.
int file_create_write(char* location_filename, char* write_data, int size)
{
int file_descriptor;
char add_write[1024]={0};
int ret;
int iCnt;
printf("\n");
if( (file_descriptor = open(location_filename,O_CREAT|O_WRONLY,0644)) > 0 )
{
printf("Open file_descriptor=%d\n",file_descriptor);
write(file_descriptor, write_data, size);
while(1)
{
printf("[ADD] Input data to file (nothing: Press Enter...) : ");
fgets(add_write, sizeof(add_write), stdin);
if( *(add_write) == '\n' )
{
break;
}
else
{
write(file_descriptor, add_write, sizeof(add_write)-1);
for(iCnt=0; iCnt<sizeof(add_write); iCnt++)
{
add_write[iCnt]=0;
}
}
}
close(file_descriptor);
}
else
{
printf("File Open Error...\n");
return -1;
}
return 0;
}//위의 함수로 쓴 파일을 열고 read하는 코드입니다.
int read_info(char* file_address, char* find_index)
{
int file_descriptor;
char file_index[1024] = {0};
int ret, iCnt=0;
printf("\n");
if( (file_descriptor = open(file_address,O_RDONLY)) > 0)
{
if( read(file_descriptor,file_index,sizeof(file_index)) > 0 )
{
puts(file_index);
}
else
{
printf("read Error!!!\n");
}
close(file_descriptor);
}
else
{
printf("File open Fail...\n");
return -1;
}
return 0;
}
int main(int argc, char* argv[])
{
if( argc == 1 )
{
printf("Argument Error... Read using manual\n");
printf("./execution_file_name (file_location) (find_index)\n");
return -1;
}
read_info(argv[1], argv[2]);
return 0;
}Forums:


파일 write하는 main함수입니다.. 질문에 올리니까 이상하게 나와서 여기다 올립니다
int main(void) { int ret,iCnt = 0; char file_location_name [1024] = {0}; char file_write [1024] = {0}; printf("\nInput File Location and file name (ex. ./test/text.c) : "); fgets(file_location_name, sizeof(file_location_name), stdin); while( *(file_location_name+iCnt) != '\n' ) { iCnt++; } *(file_location_name+iCnt) = '\0'; printf("\n Input data to file : "); fgets(file_write, sizeof(file_write), stdin); for(iCnt=0; iCnt<strlen(file_write); iCnt++) { printf("[D] file_write[%d]=%c[0x%02x]\n",iCnt,file_write[iCnt],file_write[iCnt]); } /* file_write end is '\n' */ ret = file_create_write( file_location_name, file_write, strlen(file_write) ); printf("[D] File open return=%d\n", ret); }댓글 달기