我有一个void display_a_student()使用两个二进制文件的函数 .首先是binary1.dat和index.dat,它包含添加到binary1.dat的每个学生的偏移量.
我正在尝试使用索引来查找用户输入的学生的偏移值,我在使用strcmp()函数将输入的值与index.dat文件中保存的值进行比较时遇到问题.
任何帮助将非常感谢这里是目前为止的代码.
void display_a_student()
{
struct student aStudent;
char studentNumSearch[11];
int index=0;
int found = false;
fp = fopen("binary1.dat", "a+b");
fp1 = fopen("index.dat", "a+b");
printf("\n\nWhich student are you searching for?");
scanf("%s", studentNumSearch);
fflush(stdin);
while(!found && index < 10)
{
if(strcmp(studentNumSearch,fp1[index].studentNum)==0)
{
found = true;
}
index++;
}
if (found)
{
fseek(fp, fp1[index].offset, SEEK_SET);
fread(&aStudent,sizeof(struct student),1,fp);
printf("\n\nThe student name is %s\n",aStudent.firstName);
}
else
{
printf("\n\nNo such student\n");
}
fclose( fp ); /* fclose closes file */ …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过编写C++应用程序与开源程序进行交互,该应用程序可以读取 - 修改 - 编写该程序使用的二进制文件格式.好处是程序是开源的,坏的是它的二进制文件不遵循任何"标准",只是它自己的阅读和保存方式.
这就是程序如何bool在文件中保存变量:
memset(keyword, 0, 100);
sprintf(keyword, "drawbox");
fout.write((char*)keyword, strlen(keyword)+1);
fout.write((char*)&m_drawBox, sizeof(bool));
Run Code Online (Sandbox Code Playgroud)
这是它如何阅读bool:
else if (strcmp(keyword, "drawbox") == 0)
fin.read((char*)&m_drawBox, sizeof(bool));
Run Code Online (Sandbox Code Playgroud)
这部分工作正常,我可以解析它.我也可以像整数一样解析单个变量.
我的问题在于它保存向量或四元数时的部分.
保存矢量position:
memset(keyword, 0, 100);
sprintf(keyword, "position");
fout.write((char*)keyword, strlen(keyword)+1);
fout.write((char*)&m_position, 3*sizeof(float));
Run Code Online (Sandbox Code Playgroud)
阅读矢量position:
fin.read((char*)&m_volMax, 3*sizeof(float));
else if (strcmp(keyword, "position") == 0)
Run Code Online (Sandbox Code Playgroud)
所以这些都是原始源代码的片段.在我的代码中,如果我尝试使用以下内容,我会遇到问题.
以下是我的代码中的部分:
struct Vec
{
float x,y,z;
};
Vec m_position;
else if (strcmp(keyword, "position") == 0)
fin.read((char*) &kf.m_position, 3*sizeof(float));
Run Code Online (Sandbox Code Playgroud)
但这不起作用.以下是我从示例文件中获取的值:
m_position x = -1.07374e+008
m_position y = …Run Code Online (Sandbox Code Playgroud) 我的印象是,要使用字符串打开二进制文件,您可以简单地创建字符串,然后将其实现为将读取字符串的文件的名称.这是我的讲义所说的.但是我突然失去了一些东西.我在fopen中使用了&name,name和name [SIZE],每次我都使用inBinFile == NULL,除非我使用注释行.我的字符串是正确的.怎么了?非常感谢帮助.提前致谢.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 25
int frstmenu(void);
int sndmenu(void);
int main()
{
int fmenu, smenu;
char name[SIZE];
FILE *inBinFile;
unsigned char numRead;
fmenu = frstmenu();
if ( fmenu !=1 && fmenu !=2 )
{
printf("\nIncorrect option\n");
fmenu = frstmenu();
}
if (fmenu == 1)
{
printf("\nEnter the file name: \n");
scanf("%s", &name[SIZE]);
/* printf("filename: %s", &name[SIZE]); */
smenu = sndmenu();
if (smenu !=1 && smenu !=2 )
{
printf("\nIncorrect option\n");
smenu = sndmenu();
} …Run Code Online (Sandbox Code Playgroud) 我知道文本编辑器无法读取二进制文件,只能由程序读取.但是当我使用java(字节)创建二进制文件时,我可以打开文件并读取它吗?为什么会这样?换句话说,我看到一个纯文本而不是一系列零和一个.我知道在java中有基于字节的流和基于字符的流.当我使用基于字节的流(如FileOutputStream)时,输出是字符而不是字节.
File file = new File("Myfile.dat"); // .txt or .bin
FileOutputStream fos = new FileOutputStream(file);
String data = "Hello, world";
fos.write(data.getBytes());
fos.close();
Run Code Online (Sandbox Code Playgroud)
当我使用记事本打开Myfile.dat时,我希望看到特殊字符或0和1但我可以阅读内容"hello world".所以我不清楚基于字节的流如何以二进制格式存储字符?
我有一个名为"file.wav"的wav文件,我想用C程序读取该文件的一些规范.我在这里找到了wav文件的结构.根据这个文档,在普通的wav文件中,AudioFormat字节应该是0x1和0x0,或者NumChannels字节应该是(如果是单声道)0x1和0x0; 有很多类似的部分应该看起来像这些.
现在,我的C程序非常简单:我以二进制模式打开文件,我寻找AudioFormat部分(第20个字节),我读取两个字节,然后将它们放入缓冲区; 然后我打印到stdin缓冲区内容.
#include <stdio.h>
main()
{
void *buf[2];
FILE *f;
f=fopen("file.wav", "rb");
fseek(f, 20, SEEK_SET);
fread(buf, 1, 2, f);
printf("example: %#hx %#hx\n", buf[0], buf[1]);
/*the '#' flag stands for the 0x[···] format output,
the 'h' says that the number is a short integer (2 bytes).*/
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是第二个字节而不是0x0总是0x4e0,这对于每个应该为0的字节(例如对于NumChannels或BitPerSamples字节也是如此).问题是什么?
输入文件是in.wav.我必须读取块(成功)并读取样本以规范化音频文件...
问题是,它试图FING的崩溃max和min.wav文件的采样值.它只会找到数组中的最小值和最大值,但它会崩溃 ...
请告诉我有什么问题.我认为没有理由这种行为.
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#define hdr_SIZE 64
typedef struct FMT
{
char SubChunk1ID[4];
int SubChunk1Size;
short int AudioFormat;
short int NumChannels;
int SampleRate;
int ByteRate;
short int BlockAlign;
short int BitsPerSample;
} fmt;
typedef struct DATA
{
char Subchunk2ID[4];
int Subchunk2Size;
int Data[441000];
} data;
typedef struct HEADER
{
char ChunkID[4];
int ChunkSize;
char Format[4];
fmt S1;
data S2;
} header;
int main()
{
FILE *input …Run Code Online (Sandbox Code Playgroud) 我有文件test.bin,其内容为:33 0F 13 05 54 65 73 74 20 13 06 55 73 65 72 20 31当我读到这个文件时,我得到了这样的结果:3333203046203133203035203534203635.我无法理解我做错了什么办法.
void ReadBinFile()
{
int i;
FILE *ptr;
unsigned char buffer2[17];
ptr = fopen("test.bin","r");
fread(buffer2,sizeof(buffer2),1,ptr);
for(i = 0; i<17; i++)
printf("%x", buffer2[i]);
}
Run Code Online (Sandbox Code Playgroud) 这就是我所拥有的:
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
void saveVector(vector<ObjectName*>);
vector<ObjectName*> readVector();
int main(){
vector<ObjectName*> objects;
int option;
cin >> option;
switch(option){
case 1:{
//read vector
for(int i =0; i < readVector().size(); i++){
objects.push_back(readVector()[i]);
}
break;
}
case 2:{
//save vector
objects.push_back(new ObjectName());
saveVector();
break;
}
return 0;
}
void saveVector(vector<ObjectName*> objects){
ofstream fout("./Binary/ObjectsData.bin", ios::out | ios::binary);
int size1 = objects.size();
fout.write((char*)&size1, sizeof(size1));
fout.write((char*)objects.data(), size1 * sizeof(int));
fout.flush();
fout.close();
}
vector<ObjectName*> readVector(){
vector<ObjectName*> list2;
ifstream is("./Binary/ObjectsData.bin", ios::binary);
int …Run Code Online (Sandbox Code Playgroud) 我有一个二进制数据文件.有超过4百万个LIDAR数据记录.每条记录都存储为四个数字 - 三个浮点数(坐标x,y,z)和一个整数(现在不重要).我应该实现一个在每个轴上获得最小和最大坐标的函数.我编写了下面的代码,算法看起来非常简单和简单,但它不起作用(它将每个最小值返回为0.0f,每个最大值返回1.0f).我有什么不对吗?
void get_min_max(const char *filename, float *a_min_x, float *a_max_x, float *a_min_y, float *a_max_y, float *a_min_z, float *a_max_z) {
FILE *f = NULL;
float x, y, z;
float min_x, min_y, min_z, max_x, max_y, max_z;
int l_type;
f = fopen(filename, "rb");
if (!f) {
printf("No binary file read!\n");
exit(-1);
}
min_x = min_y = min_z = std::numeric_limits<float>::max();
max_x = max_y = max_z = -std::numeric_limits<float>::max();
while (true) {
x = fread((void*)(&x), sizeof(x), 1, f);
y = fread((void*)(&y), sizeof(y), 1, f);
z …Run Code Online (Sandbox Code Playgroud) >>> data=b'\x11\x22'
>>> data.hex()
'1122'
>>> len(data)
2
#let's try to replace data ....
>>> data.replace(b'1122',b'3344').hex()
'1122'
Run Code Online (Sandbox Code Playgroud)
为什么我不能用replace0x1122替换为 0x3344 ?