gpt4 book ai didi

c - 文本到二进制数据库

转载 作者:太空宇宙 更新时间:2023-11-04 03:56:18 24 4
gpt4 key购买 nike

我需要帮助将文本文件转换为 C 的二进制文件。我无法继续我的程序,即使我已经编写了一半,因为我不知道是否正在打印任何值。转换需要最大、宽度、长度和人或动物的字符串。基本上我正在编写的程序读取二进制数据库文件并返回满足搜索的所有值。感谢您的帮助

#include <stdio.h>
#include <string.h>

typedef struct{
int max;
double length, width;
char people;
} max_t;

int
main()
{
FILE *database;
int inp, out;

database = fopen("database.txt", "r");
fscanf(database, "Max : %d", &max);
fscanf(database, "Width : %lf", &width);
fscanf(database, "Length : %lf", &length);
fscanf(database, "People : %s", &people);
fwrite(&max, 1, sizeof(max), database);
fwrite(&width, 1, sizeof(width), database);
fwrite(&length, 1, sizeof(length), database);
fwrite(&people, 1, sizeof(people), database);
database = fopen("out.txt", "wb");

return(0);
}

最佳答案

你犯了一些愚蠢的错误。您没有在输出文件中写入任何内容,而是写入以“只读”模式打开的输入文件!

#include <stdio.h>
#include <string.h>

typedef struct s{
int max;
double length, width;
char people[10];
} max_t;

int main()
{
FILE *database, *out;
//int inp, out;
max_t inp;
database = fopen("database.txt", "r");
out = fopen("out.txt", "wb");
fscanf(database, "Max : %d", &inp.max);
fscanf(database, "Width : %lf", &inp.width);
fscanf(database, "Length : %lf", &inp.length);
fscanf(database, "People : %s", inp.people);
fwrite(&inp.max, 1, sizeof(inp.max), out);
fwrite(&inp.width, 1, sizeof(inp.width), out);
fwrite(&inp.length, 1, sizeof(inp.length), out);
fwrite(inp.people, 1, sizeof(inp.people), out);
fclose(database);
fclose(out);
return(0);
}

编辑:查看 main() 和结构

关于c - 文本到二进制数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com