fread 或 fwrite 给出 "Bad file number"
fread or fwrite gives "Bad file number"
我在 Windows 8.1 中使用 MinGW,我有一个原始数字的输入文本文件(每行一个),我想将它们作为二进制文件写入一个新的二进制文件中。该示例编译时没有问题:
gcc -pedantic -Os -c my_code.c -o my_code.exe
但输出是
$ my_code.exe
sh: ./my_code.exe: Bad file number
这是我写的代码:
#include<stdio.h>
int main ()
{
FILE *fp;
FILE *prob;
int length;
char buffer[30];
// Open file containing numbers
if ((prob = fopen("raw_numbers.txt","r")) == NULL)
{
printf("Could not open raw_numbers.txt\n");
exit(1);
}
/* Create output binary file */
fp = fopen( "file.bin" , "w" );
while( ! feof(prob) )
{
fgets(buffer, sizeof(buffer), prob);
fwrite((const void*) & buffer, 1, sizeof(buffer), fp);
}
fclose(prob);
fclose(fp);
return(0);
}
使用
$ gcc --version
gcc (GCC) 3.4.4 (msys special)
您的程序中有多个错误:
您应该测试是否无法创建输出文件。
你应该测试 fgets()
的 return 值,而不是使用 while (!feof())...
,它不会像你在 Why is “while ( !feof (file) )” always wrong? 中解释的那样做
你应该将 buffer
传递给 fwrite
而不是 &buffer
您应该传递要写入的字节数 (strlen(buffer)
) 而不是缓冲区的大小。
你说输出文件应该是二进制文件,但你把它作为文本文件打开并写入文本。您的意思是将数字转换为二进制并写入二进制表示吗?
这是实现上述内容的替代方案:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
FILE *prob;
int value;
char buffer[30];
/* Open file containing numbers */
if ((prob = fopen("raw_numbers.txt", "r")) == NULL) {
printf("Could not open raw_numbers.txt: %s\n", strerror(errno));
exit(1);
}
/* Create output binary file */
if ((fp = fopen("file.bin", "wb")) == NULL) {
printf("Could not open file.bin: %s\n", strerror(errno));
exit(1);
}
while (fgets(buffer, sizeof(buffer), prob) != NULL) {
value = atoi(buffer);
if (fwrite(&value, sizeof(value), 1, fp) != 1) {
printf("Error writing to file.bin: %s\n", strerror(errno));
exit(1);
}
}
fclose(prob);
fclose(fp);
return 0;
}
shell 诊断具有误导性,但它的含义如下:文件 my_code.exe
具有无法识别为可执行文件的签名(也称为幻数)。内核无法确定如何从幻数 运行 文件,因此 错误文件号 .
原因是你的编译命令:gcc -pedantic -Os -c my_code.c -o my_code.exe
将源文件my_code.c
编译成目标格式而不是link直接编译成可执行格式。一步去掉-c
编译选项和link:
gcc -pedantic -Os my_code.c -o my_code.exe
我在 Windows 8.1 中使用 MinGW,我有一个原始数字的输入文本文件(每行一个),我想将它们作为二进制文件写入一个新的二进制文件中。该示例编译时没有问题:
gcc -pedantic -Os -c my_code.c -o my_code.exe
但输出是
$ my_code.exe
sh: ./my_code.exe: Bad file number
这是我写的代码:
#include<stdio.h>
int main ()
{
FILE *fp;
FILE *prob;
int length;
char buffer[30];
// Open file containing numbers
if ((prob = fopen("raw_numbers.txt","r")) == NULL)
{
printf("Could not open raw_numbers.txt\n");
exit(1);
}
/* Create output binary file */
fp = fopen( "file.bin" , "w" );
while( ! feof(prob) )
{
fgets(buffer, sizeof(buffer), prob);
fwrite((const void*) & buffer, 1, sizeof(buffer), fp);
}
fclose(prob);
fclose(fp);
return(0);
}
使用
$ gcc --version
gcc (GCC) 3.4.4 (msys special)
您的程序中有多个错误:
您应该测试是否无法创建输出文件。
你应该测试
fgets()
的 return 值,而不是使用while (!feof())...
,它不会像你在 Why is “while ( !feof (file) )” always wrong? 中解释的那样做你应该将
buffer
传递给fwrite
而不是&buffer
您应该传递要写入的字节数 (
strlen(buffer)
) 而不是缓冲区的大小。你说输出文件应该是二进制文件,但你把它作为文本文件打开并写入文本。您的意思是将数字转换为二进制并写入二进制表示吗?
这是实现上述内容的替代方案:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
FILE *prob;
int value;
char buffer[30];
/* Open file containing numbers */
if ((prob = fopen("raw_numbers.txt", "r")) == NULL) {
printf("Could not open raw_numbers.txt: %s\n", strerror(errno));
exit(1);
}
/* Create output binary file */
if ((fp = fopen("file.bin", "wb")) == NULL) {
printf("Could not open file.bin: %s\n", strerror(errno));
exit(1);
}
while (fgets(buffer, sizeof(buffer), prob) != NULL) {
value = atoi(buffer);
if (fwrite(&value, sizeof(value), 1, fp) != 1) {
printf("Error writing to file.bin: %s\n", strerror(errno));
exit(1);
}
}
fclose(prob);
fclose(fp);
return 0;
}
shell 诊断具有误导性,但它的含义如下:文件 my_code.exe
具有无法识别为可执行文件的签名(也称为幻数)。内核无法确定如何从幻数 运行 文件,因此 错误文件号 .
原因是你的编译命令:gcc -pedantic -Os -c my_code.c -o my_code.exe
将源文件my_code.c
编译成目标格式而不是link直接编译成可执行格式。一步去掉-c
编译选项和link:
gcc -pedantic -Os my_code.c -o my_code.exe