Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';在C中使用fread时,我无法读取二进制文件_C_Binary_Fread - Fatal编程技术网

Can';在C中使用fread时,我无法读取二进制文件

Can';在C中使用fread时,我无法读取二进制文件,c,binary,fread,C,Binary,Fread,我在C中使用fread读取二进制数据时遇到问题 这是我需要读取的二进制文件 我的问题是,文件有五行普通文本,我不知道如何避免它们或将它们作为文本读取,以及二进制部分何时开始读取二进制数据 这是我的密码: size_t num; FILE *fp = fopen("binaryXYZRGB.nkt", "rb"); if(fp == NULL){ printf("HATA!!!\n"); exit(1); } int counter = 0; while(1){ s

我在C中使用fread读取二进制数据时遇到问题

这是我需要读取的二进制文件

我的问题是,文件有五行普通文本,我不知道如何避免它们或将它们作为文本读取,以及二进制部分何时开始读取二进制数据

这是我的密码:

size_t num;
FILE *fp = fopen("binaryXYZRGB.nkt", "rb");

if(fp == NULL){
    printf("HATA!!!\n");
    exit(1);
}

int counter = 0;
while(1){

    struct read a;
    num = fread(&a, 1, sizeof(a), fp);
    printf("%d-) %f %f %f %d %d %d\n", counter, a.x, a.y, a.z, a.r, a.g, a.b);

    counter++;

    if(num < 1){
        break;
    }
}
我可能读过这样的书

但是我读到这个


如果有人能帮我,那就太好了。

正如我在屏幕截图中看到的,也得到了您的确认,在二进制数据之前有5行正常数据。。。 我们有两种选择……都涉及到将“fgets”与“fread”结合使用

“fgets”,因为它会停止并在遇到新行时记录新行。这使我们能够在使用“fread”读取二进制数据之前准确地定位文件指针。 以下是两个文件指针定位选项:

  • “ftell”和“fseek”

  • “fgetpos”和“fsetpos”

下面的代码记录了这两种方法,但我选择了“fgetpos”和“fsetpos”。 希望这些评论有用

#include <stdio.h>

int main(){
int n = 0; //to use in "ftell"
int i = 0; //as a counter
FILE * fp;
char array[100];//in case you want to store the normal characters received from "fgets"
fpos_t ptr;//used to record file pointer position in fgetpos and fsetpos
size_t num;

FILE *fp = fopen("binaryXYZRGB.nkt", "rb");

while(i < 5){//since there are 5 lines
  fgets(array+n,sizeof array,fp);//stores these lines in array
//n = ftell(fp);//use this with fseek, post 5 iterations "n" would be the offset 
  i++;          // of binary data from the start of the file.
}               

//fseek(fp,n,SEEK_SET);//can use this instead of fgetpos/fsetpos
                     //the offset "n" collected above is used
//printf("%s",array);//if array needs to be printed

fgetpos(fp,&ptr);//get the current position of the file pointer   
fsetpos(fp,&ptr);//set the current position of the file pointer

int counter = 0;
while(1){

  struct read a;
  num = fread(&a, 1, sizeof(a), fp);
  if(num < 1){ //have brought the break condition above the print statement
    break;
  }
  printf("%d-) %f %f %f %d %d %d\n", counter, a.x, a.y, a.z, a.r, a.g, a.b);
  counter++;

}
 fclose(fp);
}
#包括
int main(){
int n=0;//用于“ftell”
int i=0;//作为计数器
文件*fp;
字符数组[100];//如果要存储从“fgets”接收的普通字符
fpos_t ptr;//用于记录fgetpos和fsetpos中的文件指针位置
大小/数量;
文件*fp=fopen(“binaryXYZRGB.nkt”、“rb”);
而(i<5){//,因为有5行
fgets(array+n,sizeof array,fp);//将这些行存储在数组中
//n=ftell(fp);//将其用于fseek,在5次迭代后,“n”将是偏移量
i++;//从文件开始的二进制数据。
}               
//fseek(fp,n,SEEK_SET);//可以用它代替fgetpos/fsetpos
//使用上面收集的偏移量“n”
//printf(“%s”,数组);//如果需要打印数组
fgetpos(fp,&ptr);//获取文件指针的当前位置
fsetpos(fp,&ptr);//设置文件指针的当前位置
int计数器=0;
而(1){
结构读a;
num=fread(&a,1,sizeof(a),fp);
如果(num<1){//将中断条件置于打印语句之上
打破
}
printf(“%d-”%f%f%f%d%d%d\n),计数器,a.x、a.y、a.z、a.r、a.g、a.b);
计数器++;
}
fclose(fp);
}

文件是使用C程序生成的,该程序使用相同的
struct read
,由相同的编译器编译,用于相同的目标机器吗?否则,所有赌注都将被取消;C数据类型和数据结构的布局是特定于实现的。这就是说,只有
float
int
的结构布局将是相当可移植的,但仍然存在字节顺序问题。
.nkt
文件是否在数据之前有一些头,这样即使大部分数据与
结构兼容,您也误读了该文件?你从哪里得到这个文件的?您使用的文件格式是什么?欢迎使用SO!请您可以选择并粘贴文本来代替图像。非常感谢。嗨,卡兹,学校给了我这个文件,是的,它实际上是一个nkt文件,但我在读取数据的二进制部分时遇到了一些麻烦。因此,如果“alanalr”是“xyzrgb”,那么应该读取定义为浮点的xyz坐标和定义为整数的rgb颜色代码的数据。所以,如果二进制数据是像普通文本一样写的,它应该是100.950 100.450 100.230 1 5 10或不同的数字,所以我只想避免标题部分,我该怎么做呢?
#include <stdio.h>

int main(){
int n = 0; //to use in "ftell"
int i = 0; //as a counter
FILE * fp;
char array[100];//in case you want to store the normal characters received from "fgets"
fpos_t ptr;//used to record file pointer position in fgetpos and fsetpos
size_t num;

FILE *fp = fopen("binaryXYZRGB.nkt", "rb");

while(i < 5){//since there are 5 lines
  fgets(array+n,sizeof array,fp);//stores these lines in array
//n = ftell(fp);//use this with fseek, post 5 iterations "n" would be the offset 
  i++;          // of binary data from the start of the file.
}               

//fseek(fp,n,SEEK_SET);//can use this instead of fgetpos/fsetpos
                     //the offset "n" collected above is used
//printf("%s",array);//if array needs to be printed

fgetpos(fp,&ptr);//get the current position of the file pointer   
fsetpos(fp,&ptr);//set the current position of the file pointer

int counter = 0;
while(1){

  struct read a;
  num = fread(&a, 1, sizeof(a), fp);
  if(num < 1){ //have brought the break condition above the print statement
    break;
  }
  printf("%d-) %f %f %f %d %d %d\n", counter, a.x, a.y, a.z, a.r, a.g, a.b);
  counter++;

}
 fclose(fp);
}