C 跳过输入文件行的程序
我有一个程序,它应该问你温度,然后在普朗克函数中使用这个温度。波长位于名为“inputwave.dat”的文件中 看起来像C 跳过输入文件行的程序,c,input,output,C,Input,Output,我有一个程序,它应该问你温度,然后在普朗克函数中使用这个温度。波长位于名为“inputwave.dat”的文件中 看起来像 500 1000 1500 2000 . . . 11500 12000 (500至12000的间隔。每一条线路上) 我遇到的问题是,它只是每隔一行打印一次 就这样 "500 .... 1500 .... 2500 .... 3500 ...." 我希望它打印出每一行,在我的代码中,我似乎找不到任何会导致它跳过一行的内容 #include <stdio.h>
500
1000
1500
2000
.
.
.
11500
12000
(500至12000的间隔。每一条线路上)
我遇到的问题是,它只是每隔一行打印一次
就这样
"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);
}
#包括
#包括
#包括
双普朗克(双波,双T);
int main()
{
双波,T;
双重结果;
国际货币基金组织;
字符输出文件[80];
文件*输出,*输入;
in=fopen(“inputwave.dat”,“r”);
printf(“\n输入温度,单位为开尔文>”);
ni=扫描频率(“%lf”、&T);
printf(“\n输入输出文件的名称>”;
ni=扫描频率(“%s”,输出文件);
if((out=fopen(outfile,“w”))==NULL)
{
printf(“\n无法打开%s进行写入,\n”,输出文件);
出口(1);
}
while(fscanf(in、%lf、&wave)!=EOF)
{
fscanf(在、%d、&wave中);
结果=普朗克(波,T);
fprintf(输出,“%7.1f%e\n”,波形,结果);
}
fclose(out);
返回(0);
}
双普朗克(双波,双T)
{
静态双p=1.19106e+27;
双p1;
p1=p/(波,5.0)*(exp(1.43879e+08/(波*T))-1.0);
返回(p1);
}
谢谢您的时间。首先,请尝试以下方法(即不要给fscanf打两次电话):
之后,让我们正确检查fscanf
的返回值。此函数返回成功填充的参数列表的项数。因此,只有当此返回值正好为1时,才应执行此while的主体。因此,最好更改检查:
while(fscanf(in,"%lf",&wave) == 1)
首先,尝试一下(即不要调用fscanf
两次):
之后,让我们正确检查fscanf
的返回值。此函数返回成功填充的参数列表的项数。因此,只有当此返回值正好为1时,才应执行此while的主体。因此,最好更改检查:
while(fscanf(in,"%lf",&wave) == 1)
首先,尝试一下(即不要调用fscanf
两次):
之后,让我们正确检查fscanf
的返回值。此函数返回成功填充的参数列表的项数。因此,只有当此返回值正好为1时,才应执行此while的主体。因此,最好更改检查:
while(fscanf(in,"%lf",&wave) == 1)
首先,尝试一下(即不要调用fscanf
两次):
之后,让我们正确检查fscanf
的返回值。此函数返回成功填充的参数列表的项数。因此,只有当此返回值正好为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);
}
另外,您没有关闭代码中的输入文件。您调用了两次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);
}
另外,您没有关闭代码中的输入文件。您调用了两次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);
}
另外,您没有关闭代码中的输入文件。您调用了两次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);
}
另外,您没有在代码中关闭输入文件