我已經使用了結構來創建複數,並且該函數給出了求和的結果。之後,我試圖使用文件存儲數據在complextoplam.txt.Before中以「r」模式I在「W」模式和數據complextoplam.txt存儲的數據是帶有意想不到的無限循環的文件
10.000000 5.000000i 8.000000 9.000000i Sum=18.000000+14.000000i
存儲文本文件中的數據後,我想讀在控制檯屏幕數據和打印,但這種情況發生的錯誤當我使用fscanf和EOF進行循環時,循環沒有完成,並且它正在進入一個無限循環。爲什麼我的程序是這樣做的。我認爲你的想法會失敗我會改進我。親切的問候。
#pragma warning (disable :4996)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
struct Complex {
double real;
double img;
};
struct Complex c1, c2,c3;
void Toplam(struct Complex c1, struct Complex c2)
{
c3;
c3.img = c1.img + c2.img;
c3.real = c1.real + c2.real;
}
int main() {
/*c1, c2;
printf("Write complex c1.\n");
scanf("%lf %lf", &c1.real, &c1.img);
printf("Write complex c2.\n");
scanf("%lf %lf", &c2.real, &c2.img);
Toplam(c1, c2);
printf("Sum=%lf + %lfi", c3.real, c3.img);*/
//this part is used for only in "w" mode//
int i;
FILE *kp;
kp = fopen("complextoplam.txt", "r");
if (kp == NULL)
{
printf("File opening error.\n");
system("pause");
exit(1);
}
printf("File opened correctfully.\n");
while (fscanf(kp,"%lf %lf\n %lf %lf \n%lf %lf\n", &c1.real, &c1.img,
&c2.real, &c2.img,&c3.real, &c3.img) != EOF)
{
printf("%lf %lf %lf %lf %lf %lf", c1.real, c1.img, c2.real, c2.img,
c3.real, c3.img);
}
getch();
return 0;
}
如果文件數據不了'fscanf'格式字符串匹配,它不會返回'EOF'。它返回成功解析的項目數。 – Barmar
你真的在文件中有'Sum ='嗎?你沒有任何'fscanf()'格式來匹配它。 – Barmar
對於你'fscanf',你還缺少'''''第二個'%lf' – manman