首页 > 解决方案 > 读取浮点数的阻塞 I/O 问题 - C

问题描述

我最近一直在尝试编写一个文件写入程序,该程序可以保存零件编号、数量和零件价格的库存统计信息。在写入我的二进制文件时,我的 scanf 保存了我的价格,但是当我在下一个程序中读取它们时,它会出现大量无意义的数字,这不是我输入的。 带有编写程序的编译器:(* * 是用户输入)

This program stores a business inventory.
Please enter item data (part number, quantity, price): *2, 3, 1.6*
Please enter item data (part number, quantity, price): *3, 1, 5.3*
Please enter item data (part number, quantity, price): *0*
Thank you. Inventory stored in file inventory.txt

编写程序代码

   #include <stdio.h>
#include <stdlib.h>

int main(int argc, int argv[])
{
int pnum=1, quantity;
float price;
FILE *fp1;

fp1 = fopen("inventory.txt", "wb+");
if(fp1 == NULL)
{
    printf("Can't open!\n");
    exit(EXIT_FAILURE);
}

printf("This program stores a business inventory.\n");
while(pnum != 0)
{
printf("Please enter item data (part number, quantity, price): ");
scanf("%d, %d, %f", &pnum, &quantity, &price);
printf("%d, %d, %f", pnum, quantity, price);
fwrite(&pnum, sizeof(int), 1, fp1);// Is there a way to combine these 3 fwrites into 1?
fwrite(&quantity, sizeof(int), 1, fp1);
fwrite(&price, sizeof(float), 1, fp1);
}
printf("Thank you. Inventory stored in file inventory.txt");
fclose(fp1);
return 0;


}

带有读取程序的编译器(* * 是用户输入)

Below are the items in your inventory.
Part#    Quantity        Item Price
2         3                1070386381?
3         1                1084856730?
0?        1                1084856730?

阅读程序代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
int pnum, quantity;
float price;
FILE *fp1 = fopen("inventory.txt", "rb");
if(fp1 == NULL)
{
    printf("Can't open!");
    exit(EXIT_FAILURE);
}
printf("Below are the items in your inventory.\n");

printf("Part#\tQuantity\t Item Price\n");
while (fread(&pnum, sizeof(int), 1, fp1) == 1)//Is there a way to combine these 3 freads into 1 line of code?
{
    printf("%5d\t", pnum);
}
while (fread(&quantity, sizeof(int), 1, fp1) == 1)
{
    printf("%8d\t", quantity);
}
while (fread(&price, sizeof(float), 1, fp1) == 1)
{
    printf("$");
    printf("%9.2f\n", price);

}
fclose(fp1);
return 0;

}

如您所见,scanf 是 scanf 并且必须与我的浮动有关,但我无法弄清楚如何修复它,因为没有 scanf 什么都不会保存到我的 inventory.txt 文件中(我没有t 包含 .txt 文件,因为它是二进制文件),并且由于某种原因,当我输入 0 以中断循环时,它会将 0 保存在文件中。如果需要任何其他信息,我可以提供,但我想我已经提供了一切。感谢您的帮助,祝您编码愉快:)

标签: cfile-iowhile-loop

解决方案


  • 零被插入到数组中,因为您的while循环仅在调用后中断。fwrite您可以使用breakI believe 在scan.
  • 要组合写入和读取,您可以使用结构,但请注意,由于padding,您可能应该从/到完全相同的结构进行序列化和反序列化。
  • 怀疑fread由于错误的写入/读取顺序或填充,您的 s into float 会产生奇怪的结果。尝试读取和写入结构,看看是否能解决您的问题。如果这不起作用,请尝试使用 打印浮点数的十六进制值%x。然后与预期结果进行比较。

考虑以下代码:

#include <stdio.h>
#include <fcntl.h> // for open
#include <unistd.h> // for close


typedef struct
{
    float price;
    int pnum;
    int quantity;
} shoppingItem;


void writeToFile(FILE *fp) {    
    shoppingItem input1 = {1.1,2,3};
    shoppingItem input2 = {3.1412,42,666};
    fwrite(&input1, sizeof(shoppingItem), 1, fp);
    fwrite(&input2, sizeof(shoppingItem), 1, fp);
    printf("%f %d %d\n", input1.price, input1.pnum, input1.quantity);
    printf("%f %d %d\n", input2.price, input2.pnum, input2.quantity);
}

void readFromFile(FILE *fp) {
    shoppingItem output1 = {0.0,0,0};
    shoppingItem output2 = {0.0,0,0};
    fread(&output1, sizeof(shoppingItem), 1, fp);
    fread(&output2, sizeof(shoppingItem), 1, fp);
    printf("%f %d %d\n", output1.price, output1.pnum, output1.quantity);
    printf("%f %d %d\n", output2.price, output2.pnum, output2.quantity);
}

int main()
{
    FILE *fp = fopen("inventory.txt", "wb+");
    if(fp == NULL)
    {
        printf("Can't open!\n");
        return 1;
    }

    writeToFile(fp);
    rewind(fp);
    readFromFile(fp);

    if (fclose(fp) == 0) {
        printf("Done\n");
        return 0;
    } else {
        printf("Error on closing file\n");
        return 1;
    }
}

推荐阅读