读取二进制和 txt 文件 - 一次 1 个字节
Read binary and txt File - 1 byte at a time
我正在尝试编写一个读取 1 个字节的代码(理想目标是 n 个字节,但从 1 个字节开始 - 所以对于 n 个字节,如果它更容易,请建议)
下面是我尝试一次读取 1 个字节并以十六进制格式输出的代码。但是得到的只是一堆 FFFF
FILE *fp;
int stringlength,i;
/* File can be txt or .bin */
fp = fopen("TestFile3.txt", "r");
if (fp == NULL)
{
puts("Error: Input file cannot be read");
return -1;
}
else
{
size_t i, strlength, lengthOfFile,c;
fseek(fp, 0, SEEK_END);
lengthOfFile = ftell(fp);
printf("length of File is ---- %d \n", lengthOfFile);
while (lengthOfFile)
{
c = fgetc(fp);
printf("%c", c);
lengthOfFile--;
}
putchar('\n');
}
fclose(fp);
return 0;
}
您需要 fseek(fp, 0, SEEK_SET);
在 while
循环之前重置文件指针。
您还以 "text" 模式打开文件:
fp = fopen("TestFile3.txt", "r");
根据 C Standard, section 7.19.2:
A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined. Characters
may have to be added, altered, or deleted on input and output to
conform to differing conventions for representing text in the host
environment. Thus, there need not be a one- to-one correspondence
between the characters in a stream and those in the external
representation.
使用 fseek()/ftell()
不会 return 从文本流中读取的字节数。
如果要读取文件大小的每个字节,您需要以二进制模式打开文件:
fp = fopen("TestFile3.txt", "rb");
最后,fseek()/ftell()
在二进制文件上的使用也不可靠,因为再次根据 C 标准,7.19.9.2:
A binary stream need not meaningfully support fseek calls with a
whence value of SEEK_END
鉴于此,您也不能可靠地使用 fseek()/ftell()
来找出二进制文件有多大。是的,例子确实存在。
为了可靠地读取文件中的所有字节,@Weather Vane 在评论中发布了一种方法。
我正在尝试编写一个读取 1 个字节的代码(理想目标是 n 个字节,但从 1 个字节开始 - 所以对于 n 个字节,如果它更容易,请建议)
下面是我尝试一次读取 1 个字节并以十六进制格式输出的代码。但是得到的只是一堆 FFFF
FILE *fp;
int stringlength,i;
/* File can be txt or .bin */
fp = fopen("TestFile3.txt", "r");
if (fp == NULL)
{
puts("Error: Input file cannot be read");
return -1;
}
else
{
size_t i, strlength, lengthOfFile,c;
fseek(fp, 0, SEEK_END);
lengthOfFile = ftell(fp);
printf("length of File is ---- %d \n", lengthOfFile);
while (lengthOfFile)
{
c = fgetc(fp);
printf("%c", c);
lengthOfFile--;
}
putchar('\n');
}
fclose(fp);
return 0;
}
您需要 fseek(fp, 0, SEEK_SET);
在 while
循环之前重置文件指针。
您还以 "text" 模式打开文件:
fp = fopen("TestFile3.txt", "r");
根据 C Standard, section 7.19.2:
A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. Characters may have to be added, altered, or deleted on input and output to conform to differing conventions for representing text in the host environment. Thus, there need not be a one- to-one correspondence between the characters in a stream and those in the external representation.
使用 fseek()/ftell()
不会 return 从文本流中读取的字节数。
如果要读取文件大小的每个字节,您需要以二进制模式打开文件:
fp = fopen("TestFile3.txt", "rb");
最后,fseek()/ftell()
在二进制文件上的使用也不可靠,因为再次根据 C 标准,7.19.9.2:
A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END
鉴于此,您也不能可靠地使用 fseek()/ftell()
来找出二进制文件有多大。是的,例子确实存在。
为了可靠地读取文件中的所有字节,@Weather Vane 在评论中发布了一种方法。