C언어에서 바이너리 파일을 읽어올때 질문입니다;

mtg1의 이미지

파일의 구성은...
필드전체길이|필드1|필드2길이|필드2|필드3|필드4

이런 식으로 쓰여진 것이고요,

필드1은 int, 필드2는 char[20](20은 최대 길이입니다),
필드 3은 float, 필드 4는 char입니다.=ㅅ=;
길이 값은 short 형식이고요;

이걸 불러온다고 할 때 어떤 식으로 해야할까요;
일단 제가 짠 것은..

void load(FI fi[]){
	FILE *fp1, *fp2;
	char separators[]="|\n";
	int a=0;
	char data[100];
	int k=0,l=0;//인원,필드,데이터
	char *tok;
	if((fp1=fopen("seq(4000).dat","rb"))==NULL){
		printf("존재하지 않는 파일입니다.\n");
		exit(1);
	}
	if((fp2=fopen("input.txt","wb"))==NULL){
		printf("파일오류\n");
		exit(1);
	}
 
	while(fgets(data,sizeof(FI),fp1) != NULL ){  
        if(tok = strtok(data, separators))fi[z].l=(short)atoi(tok);
        if ( tok=strtok(NULL,separators) ) fi[z].num=atoi(tok);
        if ( tok=strtok(NULL,separators) ) fi[z].len=(short)atoi(tok);
        if ( tok=strtok(NULL,separators) ) strcpy(fi[z].name, tok);
        if ( tok=strtok(NULL,separators) ) fi[z].f = (float)atof(tok);
	if ( tok=strtok(NULL,separators) ) fi[z].s =  *tok;
	if(fi[z].l>0){
		fprintf(fp2, "%d\n", fi[z].num);
		z++;
	}
        if( z >= MAX)   break;
    }
	j=z;
	//printf("%d\n",z);
	fclose(fp1);
	fclose(fp2);
}

이런 식으로 짰는데, 읽어오는 과정에서 이상한 값들만 불러와지네요;;

도와주세요;;

givepray1111의 이미지

바이너리로 저장하셨다면 저장할 때 사용한 구조체로 구조체 변수를 선언다음 바로 그 구조체 변수에다 Read하면 끝나는데요.

굳이 위 코드처럼 진행해야하는 이유가 있나요?

mtg1의 이미지

아직 C언어에 대해서 잘 모르다보니 저런 식으로 진행한 것도 있지요;;(ㄷㄷ;)
어떤 함수를 써야하는지도 잘 모르겠고..(ㄷㄷ;;)
일단...

typedef struct field{
  short l;//전체 길이
  int num;//번호
  short len;//필드2(이름) 길이
  char name[20];//사원이름?
  float f;//평점
  char s;//성별
}FI;

이런 형태의 구조체에 넣어야하는데, char의 길이가 가변이라서 그걸 어떻게 처리해야하고,
이걸...최대 25000개 넣을 수 있게 했는데, 그보다 더 큰게 들어올 수 있어서
l(전체길이)가 다른 값이면 넣지 않고 해야하거든요;;

haze11의 이미지

char name[20]; 이.. 20바이트로 고정이잖아요. ㅡ.ㅡa
가변이 아니잖아요?

gazam의 이미지

파일에 쓸 때 가변길이로 쓰는 거라면 문제가 있을 수 있겠지만
위의 구조체의 name에 문자열을 넣고 구조체를 파일에 써버리는 것이라면
읽을 때 문제는 없습니다

goahno의 이미지

입력 파일의 내용이 일반 text형식의 문자열로 되어있다면 fgets를 사용하셔도 별 문제는 없습니다만
바이너리 형식일 경우 fgets로 내용을 읽어오면 문제가 생길 수도 있습니다.

댓글 달기

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