fwrite()和fread()don' t在c xcode中工作

发布于 2025-02-07 11:26:22 字数 583 浏览 1 评论 0原文

我注意到fread()和fwrite()在我的程序中不起作用。我写了这个小的人来演示它。

#include <stdio.h>

typedef struct Product {
    float size;
    float price;
} Product;

int main() {
    Product my_prod;
    my_prod.price = 13.2;
    my_prod.size = 10.3;

    FILE* file_in = fopen("/Users/piton/Desktop/UniverProg/Test/Test/input.txt", "w");
    if (file_in == NULL)
        printf("ERROR");

    fwrite(&my_prod, sizeof(Product), 1, file_in);
    
    fclose(file_in);
    return 0;
}

因此,我在input.txt中输出:€$ a33sa

(是的,我将文件命名为“输入”,但实际上是为了输出)

请帮助

谢谢

I noticed that fread() and fwrite() don't work in my programmes. I wrote this little one to demonstrate it.

#include <stdio.h>

typedef struct Product {
    float size;
    float price;
} Product;

int main() {
    Product my_prod;
    my_prod.price = 13.2;
    my_prod.size = 10.3;

    FILE* file_in = fopen("/Users/piton/Desktop/UniverProg/Test/Test/input.txt", "w");
    if (file_in == NULL)
        printf("ERROR");

    fwrite(&my_prod, sizeof(Product), 1, file_in);
    
    fclose(file_in);
    return 0;
}

So, I have output in input.txt: ÕÃ$A33SA

(Yeah, I named file "input" but actually it is for output)

Please help

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2025-02-14 11:26:22

即使您暗示您的文件是文本文件(input.txt),但使用“ fwrite”函数与包含浮点变量的结构的输出的输出,将使用存储浮点值所需的字节数来输出数据以二进制方式。对于大多数C程序将是四个字节。因此,使用您的程序,我运行了程序,然后使用十六进制文件查看器审查了原始的十六进制数据。这就是我看到的。

CD CC 24 41  33 33 53 41

八个字节与两个存储的十进制数的长度一致。 “ CD CC 24 41”表示值“ 13.2”和“ 33 33 53 41”表示值“ 10.3”。为了验证这一点,我在您的程序中添加了几行代码,以便该程序可以从文件中读取数据并打印出该文件中存储的值。

#include <stdio.h>

typedef struct Product
{
    float size;
    float price;
} Product;

int main()
{
    Product my_prod;
    my_prod.price = 13.2;
    my_prod.size = 10.3;
    
    FILE* file_out = fopen("input.txt", "w"); /* I changed the name to file.out */
    if (file_out == NULL)
        printf("ERROR");
    
    fwrite(&my_prod, sizeof(Product), 1, file_out);
    
    fclose(file_out);
    
    FILE* file_in = fopen("input.txt", "r"); /* I then reopened the file to read */
    fread(&my_prod, sizeof(struct Product), 1, file_in);
    
    printf("Price: %f, Size: %f\n", my_prod.price, my_prod.size);
    
    fclose(file_in);
    
    return 0;
}
 

当我运行程序时,这是来自文件读取的数据的输出。

Price: 13.200000, Size: 10.300000

因此,数据最初是正确存储的。

如评论中指出的那样,由于数据是以二进制方式存储的,因此您可能需要以二进制文件打开该文件(例如fopen(“ input.txt”,“ wb”))。

我希望这阐明了事情。

问候。

Even though you are implying that your file is a text file (input.txt), using the "fwrite" function with the output of a structure containing floating point variables will output the data using the number of bytes required to store a floating point value in a binary fashion. For most C programs that will be four bytes. So, using your program, I ran the program and then reviewed the raw hexadecimal data using a hexadecimal file viewer. This is what I viewed.

CD CC 24 41  33 33 53 41

The eight bytes coincides with the length of two stored decimal numbers. "CD CC 24 41" represents the value "13.2" and "33 33 53 41" represents the value "10.3". To verify this I added a few lines of code to your program so that the program would read the data back in from the file and print out the values stored in that file.

#include <stdio.h>

typedef struct Product
{
    float size;
    float price;
} Product;

int main()
{
    Product my_prod;
    my_prod.price = 13.2;
    my_prod.size = 10.3;
    
    FILE* file_out = fopen("input.txt", "w"); /* I changed the name to file.out */
    if (file_out == NULL)
        printf("ERROR");
    
    fwrite(&my_prod, sizeof(Product), 1, file_out);
    
    fclose(file_out);
    
    FILE* file_in = fopen("input.txt", "r"); /* I then reopened the file to read */
    fread(&my_prod, sizeof(struct Product), 1, file_in);
    
    printf("Price: %f, Size: %f\n", my_prod.price, my_prod.size);
    
    fclose(file_in);
    
    return 0;
}
 

When I ran the program, here is the output from the data read from the file.

Price: 13.200000, Size: 10.300000

So the data was properly stored originally.

As noted in the comments, since data is being stored in a binary fashion, you might want to open the file as a binary file (e.g. fopen("input.txt", "wb")).

I hope that clarifies things.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文