跳过输入文件行的程序
Program skipping over lines of input file
我有这个程序应该询问你温度,然后在普朗克函数中使用该温度。波长位于名为 "inputwave.dat" 的文件中
看起来像
500
1000
1500
2000
.
.
.
11500
12000
(间隔 500 到 12,000。每行各占一行)
我遇到的问题是每隔一行打印一次。
很喜欢
"500 ....
1500 ....
2500 ....
3500 ...."
我希望它打印出每一行,在我的代码中发生这种情况的地方我似乎找不到任何会导致它跳过一行的内容。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double planck(double wave, double T);
int main()
{
double wave,T;
double result;
int ni;
char outfile[80];
FILE *out,*in;
in = fopen("inputwave.dat","r");
printf("\nEnter the temperature in Kelvin > ");
ni = scanf("%lf",&T);
printf("\nEnter the name of the output file > ");
ni = scanf("%s",outfile);
if((out = fopen(outfile,"w")) == NULL)
{
printf("\nCannot open %s for writing\n",outfile);
exit(1);
}
while(fscanf(in,"%lf",&wave) != EOF)
{
fscanf(in,"%d",&wave);
result = planck(wave,T);
fprintf(out,"%7.1f %e\n",wave,result);
}
fclose(out);
return(0);
}
double planck(double wave, double T)
{
static double p = 1.19106e+27;
double p1;
p1 = p/(pow(wave,5.0)*(exp(1.43879e+08/(wave*T)) - 1.0));
return(p1);
}
感谢您的宝贵时间。
首先,试试这个(即不要调用 fscanf
两次):
while(fscanf(in,"%lf",&wave) != EOF)
{
result = planck(wave,T);
fprintf(out,"%7.1f %e\n",wave,result);
}
之后让我们正确检查 fscanf
的 return 值。此函数 returns 成功填充参数列表的项目数。因此,仅当此 return 值恰好为 1 时,才应执行此 while
的主体。因此,最好更改检查:
while(fscanf(in,"%lf",&wave) == 1)
您调用 fscanf
两次。而这一行 -
fscanf(in,"%d",&wave); // passing wrong argument to printf %d expects a integer you pass a double
试试这个 -
while(fscanf(in,"%lf",&wave)==1)
{
result = planck(wave,T);
fprintf(out,"%7.1f %e\n",wave,result);
}
此外,您没有在代码中关闭输入文件。
我有这个程序应该询问你温度,然后在普朗克函数中使用该温度。波长位于名为 "inputwave.dat" 的文件中 看起来像
500
1000
1500
2000
.
.
.
11500
12000
(间隔 500 到 12,000。每行各占一行)
我遇到的问题是每隔一行打印一次。
很喜欢
"500 ....
1500 ....
2500 ....
3500 ...."
我希望它打印出每一行,在我的代码中发生这种情况的地方我似乎找不到任何会导致它跳过一行的内容。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double planck(double wave, double T);
int main()
{
double wave,T;
double result;
int ni;
char outfile[80];
FILE *out,*in;
in = fopen("inputwave.dat","r");
printf("\nEnter the temperature in Kelvin > ");
ni = scanf("%lf",&T);
printf("\nEnter the name of the output file > ");
ni = scanf("%s",outfile);
if((out = fopen(outfile,"w")) == NULL)
{
printf("\nCannot open %s for writing\n",outfile);
exit(1);
}
while(fscanf(in,"%lf",&wave) != EOF)
{
fscanf(in,"%d",&wave);
result = planck(wave,T);
fprintf(out,"%7.1f %e\n",wave,result);
}
fclose(out);
return(0);
}
double planck(double wave, double T)
{
static double p = 1.19106e+27;
double p1;
p1 = p/(pow(wave,5.0)*(exp(1.43879e+08/(wave*T)) - 1.0));
return(p1);
}
感谢您的宝贵时间。
首先,试试这个(即不要调用 fscanf
两次):
while(fscanf(in,"%lf",&wave) != EOF)
{
result = planck(wave,T);
fprintf(out,"%7.1f %e\n",wave,result);
}
之后让我们正确检查 fscanf
的 return 值。此函数 returns 成功填充参数列表的项目数。因此,仅当此 return 值恰好为 1 时,才应执行此 while
的主体。因此,最好更改检查:
while(fscanf(in,"%lf",&wave) == 1)
您调用 fscanf
两次。而这一行 -
fscanf(in,"%d",&wave); // passing wrong argument to printf %d expects a integer you pass a double
试试这个 -
while(fscanf(in,"%lf",&wave)==1)
{
result = planck(wave,T);
fprintf(out,"%7.1f %e\n",wave,result);
}
此外,您没有在代码中关闭输入文件。