user-0 提问: fwrite成功后，fread出现问题，总是少一字节？

回复:
user: user-1
content: gdata.data = 12;  你把12 改成别的试试，比如14.

user: user-0
content: #include <stdio.h> #include <unistd.h> #include <string.h>  typedef struct { 	int data; } data_t;  data_t gdata; int main() { 	FILE *file; 	int size = 0, size_1 = 0; 	int len = 0, len_1 = 0; 	 	data_t local_data;  	memset((void *)&gdata, 0, sizeof(gdata)); 	gdata.data = 12; 	 	file = fopen("data.ini", "wb+"); 	if(file == NULL){ 		printf("fopen file ERROR"); 	} 	 	size = sizeof(gdata); 	fprintf(file, "%d\n", size); 	 	if((len = fwrite(&gdata, 1, size, file)) < size)    	{     	printf("fwrite error");     	fclose(file);     	return -1;     } 	printf("len %d write %d bytes finished.\n", len, size); 	printf("ftel result: %d\n", ftell(file)); 	fclose(file);  	getchar();  	 	file = fopen("data.ini", "rb"); 	if(file == NULL){ 		printf("fopen file ERROR"); 	} 	 	fscanf(file, "%d\n", &size_1); //	fseek(file, 2, SEEK_SET);			// 不加这行，读取不成功总是少读取一个字节		 	printf("read ftell: %d\n", ftell(file)); 	if((len_1 = fread(&local_data, 1, size_1, file)) < size_1){ 		printf("read ftell end: %d\n", ftell(file)); 		printf("fread failed, size:%d, len :%d!\n", size_1, len_1);         fclose(file);         return -1; 	} 	printf("read ftell end: %d\n", ftell(file)); 	printf("read %d bytes finished.\n", len_1); 	 	fclose(file);  	printf("%d\n", local_data.data); 	 	return 1; 	 }     不加  fseek(file, 2, SEEK_SET); 结果为： len 4 write 4 bytes finished. ftel result: 6  read ftell: 3 read ftell end: 6 fread failed, size:4, len :3!  加完之后结果正常： len 4 write 4 bytes finished. ftel result: 6  read ftell: 2 read ftell end: 6 read 4 bytes finished. 12 为什么会是这样的结果哦？

user: user-2
content: 为什么不用ftell函数看看当前读到哪儿了呢？

user: user-2
content: 电脑内存或文件内容只是一个一维二进制字节数组及其对应的二进制地址； 人脑才将电脑内存或文件内容中的这个一维二进制字节数组及其对应的二进制地址的某些部分看成是整数、有符号数/无符号数、浮点数、复数、英文字母、阿拉伯数字、中文/韩文/法文……字符/字符串、汇编指令、函数、函数参数、堆、栈、数组、指针、数组指针、指针数组、数组的数组、指针的指针、二维数组、字符点阵、字符笔画的坐标、黑白二值图片、灰度图片、彩色图片、录音、视频、指纹信息、身份证信息……  推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。

user: user-0
content: 还不是很懂 ~~~~(>_<)~~~~ 这是读取失败的写文件后的二进制文件： [root@localhost samba]# hexdump data.ini  0000000 0a34 000c 0000                          0000006 [root@localhost samba]#   如果在结构中加一个字段则能读取成功，很是不解   #include <stdio.h> #include <unistd.h> #include <string.h>  typedef struct { 	char ch;	// 如果加上ch字段，读取文件成功 	int data; } data_t;  data_t gdata; int main() { 	FILE *file; 	int size = 0, size_1 = 0; 	int len = 0, len_1 = 0; 	 	data_t local_data;  	memset((void *)&gdata, 0, sizeof(gdata)); 	gdata.data = 12; 	gdata.ch = '8'; 	 	file = fopen("data.ini", "wb+"); 	if(file == NULL){ 		printf("fopen file ERROR"); 	} 	 	size = sizeof(gdata); 	fprintf(file, "%d\n", size); 	 	if((len = fwrite(&gdata, 1, size, file)) < size)    	{     	printf("fwrite error");     	fclose(file);     	return -1;     } 	printf("len %d write %d bytes finished.\n", len, size); 	printf("ftel result: %d\n", ftell(file)); 	fclose(file);  	getchar();  	 	file = fopen("data.ini", "rb"); 	if(file == NULL){ 		printf("fopen file ERROR"); 	} 	 	fscanf(file, "%d\n", &size_1); //	fseek(file, 2, SEEK_SET);			// 删除该行，在结构体中加字段	 	printf("read ftell: %d\n", ftell(file)); 	if((len_1 = fread(&local_data, 1, size_1, file)) < size_1){ 		printf("read ftell end: %d\n", ftell(file)); 		printf("fread failed, size:%d, len :%d!\n", size_1, len_1);         fclose(file);         return -1; 	} 	printf("read ftell end: %d\n", ftell(file)); 	printf("read %d bytes finished.\n", len_1); 	 	fclose(file);  	printf("data: %d\n", local_data.data); 	printf("ch: %c\n", local_data.ch); 	 	return 1; 	 }     这行结果如下：  len 8 write 8 bytes finished. ftel result: 10  read ftell: 2 read ftell end: 10 read 8 bytes finished. data: 12        ch: 8 [root@localhost samba]#  [root@localhost samba]#  二进制文件内容为： [root@localhost samba]# hexdump data.ini  0000000 0a38 0038 0000 000c 0000                000000a [root@localhost samba]#   这里的0a38 具体指什么意思呀？

user: user-2
content: Format Specification Fields: scanf and wscanf Functions A format specification has the following form:  % [width] [{h | l | I64 | L}]type  The format argument specifies the interpretation of the input and can contain one or more of the following:   White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.   Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates.   Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.  The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.  When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.  An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.  Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.   The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.  An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.

user: user-2
content: 你以为 47:      fscanf(file, "%d\n", &size_1); 之后ftell应该为2；而显然实际为3    不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n，写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fseek,ftell,fread,fwrite,fgetc,fclose //不作以上替换，遇到\x1a仍继续读;读写的内容当字节看待 弄混了