c - 要么我的二进制文件未填充,要么我无法从中读取

标签 c

我无法从我的 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/

相关文章:

c - 函数调用后变量值发生变化,未被使用

c - 声明指向从结构定义中接受结构的函数的指针?

c - 如何让程序不断要求用户在C编程中输入值

c - 尝试做一些 C 编程作业并需要一些指导

c - 为什么在写入使用字符串初始化的 "char *s"时会出现段错误,而不是 "char s[]"?

c - 如何在数组中间重新分配()?

c - 如何将对话框位置坐标复制到另一个对话框中?

c - 低级 C - 逐像素显示文本

c - 使用 xusb.c 示例代码遇到 libusb 中逻辑 block 地址的偏移量?

python - 来自 Python : sub. stdin.write IOError Broken Pipe 的 C 子进程