linux-读取/写入C中的二进制文件
有没有人有可以写入二进制文件的代码示例。 还有可以读取二进制文件并输出到屏幕的代码。 看例子,我可以写文件了,但是当我尝试从文件中读取文件时,它不能正确输出。
4个解决方案
86 votes
读写二进制文件与其他文件几乎相同,唯一的区别是打开方式:
unsigned char buffer[10];
FILE *ptr;
ptr = fopen("test.bin","rb"); // r for read, b for binary
fread(buffer,sizeof(buffer),1,ptr); // read 10 bytes to our buffer
您说可以读取它,但是它不能正确输出...请记住,“输出”此数据时,您不是在读取ASCII,所以这不像在屏幕上打印字符串:
for(int i = 0; i<10; i++)
printf("%u ", buffer[i]); // prints a series of bytes
写入文件几乎相同,除了您使用的是fwrite()而不是printf:
FILE *write_ptr;
write_ptr = fopen("test.bin","wb"); // w for write, b for binary
fwrite(buffer,sizeof(buffer),1,write_ptr); // write 10 bytes from our buffer
由于我们在谈论Linux ..,因此有一种简单的方法可以进行完整性检查。 在系统上安装printf(如果尚未安装在系统上)并转储文件:
mike@mike-VirtualBox:~/C$ hexdump test.bin
0000000 457f 464c 0102 0001 0000 0000 0000 0000
0000010 0001 003e 0001 0000 0000 0000 0000 0000
...
现在将其与您的输出进行比较:
mike@mike-VirtualBox:~/C$ ./a.out
127 69 76 70 2 1 1 0 0 0
嗯,也许可以将printf更改为%x以使其更加清晰:
mike@mike-VirtualBox:~/C$ ./a.out
7F 45 4C 46 2 1 1 0 0 0
你看! 数据现在匹配*。 太棒了,我们必须正确读取二进制文件!
*请注意,字节只是在输出上交换,但是数据正确,您可以针对这种情况进行调整
Mike answered 2020-01-29T22:25:01Z
1 votes
我对“制作弱密码存储程序”解决方案感到非常满意。 也许它将帮助需要遵循一个非常简单的二进制文件IO示例的人们。
$ ls
WeakPin my_pin_code.pin weak_pin.c
$ ./WeakPin
Pin: 45 47 49 32
$ ./WeakPin 8 2
$ Need 4 ints to write a new pin!
$./WeakPin 8 2 99 49
Pin saved.
$ ./WeakPin
Pin: 8 2 99 49
$
$ cat weak_pin.c
// a program to save and read 4-digit pin codes in binary format
#include
#include
#define PIN_FILE "my_pin_code.pin"
typedef struct { unsigned short a, b, c, d; } PinCode;
int main(int argc, const char** argv)
{
if (argc > 1) // create pin
{
if (argc != 5)
{
printf("Need 4 ints to write a new pin!\n");
return -1;
}
unsigned short _a = atoi(argv[1]);
unsigned short _b = atoi(argv[2]);
unsigned short _c = atoi(argv[3]);
unsigned short _d = atoi(argv[4]);
PinCode pc;
pc.a = _a; pc.b = _b; pc.c = _c; pc.d = _d;
FILE *f = fopen(PIN_FILE, "wb"); // create and/or overwrite
if (!f)
{
printf("Error in creating file. Aborting.\n");
return -2;
}
// write one PinCode object pc to the file *f
fwrite(&pc, sizeof(PinCode), 1, f);
fclose(f);
printf("Pin saved.\n");
return 0;
}
// else read existing pin
FILE *f = fopen(PIN_FILE, "rb");
if (!f)
{
printf("Error in reading file. Abort.\n");
return -3;
}
PinCode pc;
fread(&pc, sizeof(PinCode), 1, f);
fclose(f);
printf("Pin: ");
printf("%hu ", pc.a);
printf("%hu ", pc.b);
printf("%hu ", pc.c);
printf("%hu\n", pc.d);
return 0;
}
$
Gregory Fenn answered 2020-01-29T22:25:21Z
0 votes
这是读取和写入二进制jjpg或wmv视频文件的示例。 FILE * fout; 文件* fin;
Int ch;
char *s;
fin=fopen("D:\\pic.jpg","rb");
if(fin==NULL)
{ printf("\n Unable to open the file ");
exit(1);
}
fout=fopen("D:\\ newpic.jpg","wb");
ch=fgetc(fin);
while (ch!=EOF)
{
s=(char *)ch;
printf("%c",s);
ch=fgetc (fin):
fputc(s,fout);
s++;
}
printf("data read and copied");
fclose(fin);
fclose(fout);
bpn answered 2020-01-29T22:25:41Z
0 votes
该问题与如何在C上写入二进制数据文件并使用CAMILO HG的Gnuplot对其进行绘制有关。 我知道真正的问题有两个部分:1)编写二进制数据文件,2)使用Gnuplot对其进行绘图。
第一部分在这里已经很清楚地回答了,所以我没有什么要补充的。
第二,简单的方法是将人员发送到Gnuplot手册,我确定有人找到了很好的答案,但是我在网络上找不到它,所以我将解释一个解决方案(该解决方案必须是真实的 问题,但我是stackoverflow的新手,无法在那里回答):
使用fwrite()写入二进制数据文件后,应使用C语言(阅读器)创建一个非常简单的程序。 读取器仅包含与写入器相同的结构,但是您使用fread()而不是fwrite()。因此,生成此程序非常容易:在reader.c文件中复制原始代码的写入部分,并更改写入以读取(和“ wb” 代表“ rb”)。 此外,您还可以对数据进行一些检查,例如,文件长度是否正确。 最后,您的程序需要使用printf()在标准输出中打印数据。
明确一点:您的程序运行如下
$ ./reader data.dat
X_position Y_position (it must be a comment for Gnuplot)*
1.23 2.45
2.54 3.12
5.98 9.52
Okey,使用此程序,在Gnuplot中,您仅需将阅读器的标准输出通过管道传输到Gnuplot,如下所示:
plot '< ./reader data.dat'
这行代码,运行程序阅读器,输出与Gnuplot连接并绘制数据。
*由于Gnuplot将要读取程序的输出,因此您必须知道Gnuplot可以读取和绘图的内容以及哪些内容不能读取和绘图。