str[i]>57)
{
//cout<<"cant convert to number"<<endl;
return -1;
}
number+=(str[i]-48)*pow(10.0,(double)dot_index-1-i);
}
for(int i=dot_index+1;i<cout;i++)
{
if(str[i]<48
------解决方案--------------------str[i]>57)
{
//cout<<"cant convert to number"<<endl;
return -1;
}
number+=(str[i]-48)*pow(10.0,(double)dot_index-i);
}
return number;
}
}
list.txt文件内容
//total length l: 1
//total time tpmax:1
//time step interval dt: 1.0e-6
//define cell number n: 100
//choice of boundary conditions(1: DIR; 2: ZGD; 3: PER) B: 1
//choice of convection types(1: UDS; 2: CDS; 3: TVD) C: 1
//switch on diffusion?(diffusion: 1; no diffusion: 0) dif: 0
//switch on convection?(convection: 1; no convection: 0) con: 1
------解决方案--------------------#include <stdio.h>
double l, tpmax, dt;
int n, B, C, dif, con;
#define MAXL 1000
char buf[MAXL];
FILE *f;
char *p;
int h;
int main() {
f=fopen("parameter.txt","r");
if (NULL==f) {
printf("Can not open file parameter.txt\n");
return 1;
}
h=0;
while (1) {
if (NULL==fgets(buf,MAXL,f)) break;
p=strrchr(buf,":");
if (p) {
switch (h) {
case 0:if (1!=sscanf(p+1,"%lf",&l )) printf("read l error!\n"); break;
case 1:if (1!=sscanf(p+1,"%lf",&tpmax)) printf("read tpmax error!\n"); break;
case 2:if (1!=sscanf(p+1,"%lf",&dt )) printf("read dt error!\n"); break;
case 3:if (1!=sscanf(p+1,"%d" ,&n )) printf("read n error!\n"); break;
case 4:if (1!=sscanf(p+1,"%d" ,&B )) printf("read B error!\n"); break;
case 5:if (1!=sscanf(p+1,"%d" ,&C )) printf("read C error!\n"); break;