나는 존 케이지 응답 (나를 그를하지 upvote에!)에 동의 또한
주의
그 (오래 전에), 바이너리 파일을 필요로 윈도우/DOS 시스템 'WB', 'RB'로 열 수 'w'와 'r'대신에. 나는 그것이 아직도 사실이지만, 아무런 문제도 없다고 확신하지 않는다.
short int x= 254;
int nwritten;
FILE * f1 = fopen("infile" ,"w+b");
nwritten=fwrite (&x , 1 , sizeof(short int) , f1); /* check number of shorts written */
if (nwritten != 1) fprintf(stderr,"Error: %d short written\n",nwritten);
을 그리고 독서 부분 : 여기
코드의 비트가 일을 더 명확 얻을 수있어 또한
short int y ;
int nread;
nread=fread(&y , sizeof(short int), 1 ,f1); /* check number of shorts read */
if (nread == 1) printf("%d" , y);
else fprintf(stderr,"Error: could not read 1 short int (%d read)\n",nread);
이 파일 내부의 위치는 각 읽기 후에 증가 기억/쓰다. 읽기 전에 파일의 시작 부분으로 돌아가고 싶을 수도 있습니다.
이
fseek(f1,0L,SEEK_SET); /* you also can use rewind(f1); for the same result */
전체 코드 :
short int x= 254;
int nwritten;
FILE * f1 = fopen("infile" ,"w+b");
nwritten=fwrite (&x , 1 , sizeof(short int) , f1); /* check number of shorts written */
if (nwritten != 1) fprintf(stderr,"Error: %d short written\n",nwritten);
short int y ;
int nread;
fseek(f1,0L,SEEK_SET); /* get back to the start */
nread=fread(&y , sizeof(short int), 1 ,f1); /* check number of shorts read */
if (nread == 1) printf("%d" , y);
else fprintf(stderr,"Error: could not read 1 short int (%d read)\n",nread);
덕분에 선생님 – mainajaved