write로 데이터 쓴 파일을 read 하면 잘 나오다가 어느 순간부터 NULL로 찍힙니다.

willisone의 이미지

파일을 생성해서 데이터를 쓸 때 모두 잘 들어갔습니다. 배열 하나하나 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;
}
willisone의 이미지

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);
 
 
}

댓글 달기

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