gpt4 book ai didi

c - 读取包含奇数字符的 DAT 文件

转载 作者:行者123 更新时间:2023-11-30 16:53:16 24 4
gpt4 key购买 nike

我正在尝试用 C 读取和打印 dat 文件的内容。我仍在学习如何读取二进制文件,我认为读取文件的正确方法是逐字节读取(如下所示)代码片段):

unsigned char buffer;
FILE *fp;
fp = fopen("products.dat", "rb");

while (!feof(fp)) {
fread(&buffer, 1, 1, fp);
printf("%c", buffer);
}

这是我收到的输出:

    16010719-5109ABSORB leathercare setq�8�q�Њq�e����(\�@16011030-0456ANNO INEZ panel curtain�8�q�Њq�eH
ףp=
@16020623-8644ALVE deskpanel curtain�8�q�Њq�eq�Q��k!@16021117-5765ALVE add-on-unit for secretaryЊq�e\�(\���@16071127-9620ANTONIUS height adjustable clothes dryerЊq�e�
ףp=
@16080205-0344BEDDINGE seriesadjustable clothes dryerЊq�e�)\���(@16090505-4102ANDY drawer unit on casterslothes dryerЊq�e[\���(\@16100207-7038BEATA collectionon casterslothes dryerЊq�e��z�G��?16140311-3192BILLY systemionon casterslothes dryerЊq�e�\���(\!@16140312-0502ALVE laptop tablen casterslothes dryerЊq�e�H�z�G@16150309-1686AKURUM wall top cabinet framethes dryerЊq�e1�p=
ף@16150613-8211BEHANDLA seriescabinet framethes dryerЊq�e1)\���("@16150701-5897ANTONIUS shelfcabinet framethes dryerЊq�e��Q��@16150807-2103ANEBODA seriescabinet framethes dryerЊq�e���Q�� @16171215-4812ANTONIUS drying racket framethes dryerЊq�e�=
ףp=@16190603-3731ABSORB leathercleanert framethes dryerЊq�e���(\��#@16200211-9747BEATA ORKIDE duvet cover and pillowcase(s)Њq�e�������@16220729-5714ADMETE RUND chair pader and pillowcase(s)Њq�e_
ףp=
�?16240510-6077ANTIFONI floor/reading lamp pillowcase(s)Њq�es{�G�z@16270718-1760ALG mirroroor/reading lamp pillowcase(s)Њq�e�fffff� @@

我应该能够从此文件中获取产品信息:产品代码、名称、数量和价格。我读取此文件的代码不正确吗?

这是 products.dat 文件内容 1]

最佳答案

You'll most likely want a structure that matches that format and then you read the data into it.

您应该遵循退休忍者的好建议。首先:

    struct product { char code[15], name[51];
short quant;
double price;
} buffer;
while (fread(&buffer, sizeof buffer, 1, fp))
printf("%s\t%-51s%3d %.2f\n", buffer.code, buffer.name,
buffer.quant, buffer.price);

关于c - 读取包含奇数字符的 DAT 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951975/

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