2017-10-16 25 views
-1
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)); 

我不得不寫程序(爲我的家庭作業使用()釋放,FWRITE(),FTELL(),FSEEK()),其中寫到二進制一些數據,然後讀取它來計算表達式。我有個問題。 我必須寫三個fwrite()和三個已釋放()嗎?或者我可以在一個函數調用中做到這一點?有沒有一個完美的方式來做到這一點?讀取和/寫入到.bin文件用C

回答

0

Here是你回答「如何寫得更好」 只需使用一個數組而不是逐個參數。 關於.bin文件這不是一個擴展名,相同的解決方案與.txt,甚至沒有擴展。