我无法从我的 bin 文件中读取...屏幕上什么也没有显示,好像里面什么也没有。 我在尝试读取它时做错了什么吗?
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <ctype.h>
#define pause system("pause")
#define cls system("cls")
#define COLS 10
#define ROWS 500
void displayTheResult(float a[][COLS], int c){
int i,j;
FILE *myBinFile;
myBinFile = fopen("c:\myBinFile.bin","rb");
if(myBinFile == NULL){
printf("ERROR Reading from Bin file\n");
printf("My computer uses 1 slash after c: \n if your computer uses 2 slashes please change all the \nfopen function accordingly in the displayTheResults() function");
pause;
exit(-1);
}//END if
fread(a,sizeof(float)*COLS, c,myBinFile);
fclose(myBinFile);
}
还是我写的时候做错了什么?? 最初,我遇到错误是因为我的老师教我使用“ba”,然后我通过谷歌发现它实际上是“ab”。
我想知道是否还有其他类似的问题。我很确定这正是我的教授教我的方式。
我想知道它会是什么。我做了错误检查,所以很难相信 BIN 只是 NULL
if( (_access( "c:\myBinFile.bin", 0 ))!= -1 )
{
printf("BIN EXISTS");
pause;
myBinFile = fopen("c:\myBinFile.bin","ab");
if(myBinFile == NULL){
printf("ERROR appending to bin File\n");
printf("My computer uses 1 slash after c: \n if your computer uses 2 slashes please change the \nfopen function accordingly in the displayTheResults() function");
pause;
exit(-1);
}//END if
fwrite (a,sizeof(float)*COLS, c,myBinFile);
fclose(myBinFile);
}//end if
else
{
printf("BIN NO EXISTS :(");
myBinFile = fopen("c:\myBinFile.bin","wb");
if(myBinFile == NULL){
printf("ERROR Creating new Bin File\n");
printf(" My computer uses 1 slash after c: \n if your computer uses 2 slashes please change the \nfopen function accordingly in the displayTheResults() function");
pause;
exit(-1);
}//END if
fwrite (a,sizeof(float)*COLS, c,myBinFile);
fclose(myBinFile);
}//end else
还是两者兼而有之?
你们要相信我。两个反斜杠在我的电脑上不起作用。我认为我的 Visual Studio 会自动修复它,但无论出于何种原因,其他计算机上都需要两个反斜杠,而 一个反斜杠在我的电脑上有效。使用两个黑斜杠我总是得到空错误并且它没有被创建。但是只用一个反斜杠就创建了文件,如果它是文本文件,我可以读取它。稍后我将在我的学校计算机上尝试此操作,该计算机使用两个反斜杠,但在那之前,有人看到代码的另一个问题吗?
最佳答案
要在字符串文字中使用反斜杠
"c:\\myBinFile.bin"
更新:
如果您使用仅一个反斜杠,编译器将发出类似*1的警告
unrecognized character escape sequence
或
unknown escape sequence
并忽略单个反斜杠,这将使文件名变为:
"c:myBinFile.bin"
这是在您启动程序的目录中名为 "myBinFile.bin"
的文件,假设该程序没有调用 chmod()
来更改工作目录.
*1 至少 VC10 和 gcc 这样做。
关于c - 要么我的二进制文件未填充,要么我无法从中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21683876/