gpt4 book ai didi

c - 用 C 读取和写入 .bin 文件

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

pInput = fopen("input.bin", "w+b");
if (pInput == NULL)
{
return(-1);
MyLogs(szError);
}

printf("Enter 'x': "); scanf("%lf", &x);

fwrite(&a, sizeof(double), 1, pInput);
fwrite(&x, sizeof(double), 1, pInput);
fwrite(&b, sizeof(double), 1, pInput);

fseek(pInput, 0, SEEK_SET);

fread(&a, sizeof(double), 1, pInput);
fread(&x, sizeof(double), 1, pInput);
fread(&b, sizeof(double), 1, pInput);
fclose(pInput);

char buff[100];
snprintf(buff, 99, "Parameter file 'input.bin' opened. X = %.3lf, a = %.0lf, b = %.0lf.", x, a, b);

MyLogs(buff);

dResult = x / a - 1 / (a * 6) * log10(a + b*exp(6 * x));

我必须编写程序(使用 freed()、fwrite()、ftell()、fseek() 来完成我的家庭作业),该程序将一些数据写入二进制文件,然后读取它进行计算表达。我有个问题。 我必须编写三个 fwrite() 和三个 freed() 吗?或者我可以在一个函数调用中完成此操作吗?有没有完美的方法来做到这一点?

最佳答案

Here你回答的是“如何写得更好”只需使用数组而不是一一参数。关于 .bin 文件,这不是扩展名,相同的解决方案适用于 .txt,甚至没有扩展名。

关于c - 用 C 读取和写入 .bin 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46773566/

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