Originally Posted by
NickHolmes
1) If I want to save the parameters in a series of arrays how do I include it? tried aa[h]=conrho; ab[h]=conmw; etc
but it gave me an error when run.
I'd recommend a single array of a structure containing all common data.
Code:
#include <stdio.h>
struct con
{
int h;
char name[20];
float conc;
double rho, mw, prod, C, D, A, B;
};
struct con mycon[5];
int main(void)
{
FILE *file = fopen("cond.inp", "r");
if ( file )
{
size_t i, j;
for ( i = 0; i < sizeof mycon / sizeof *mycon; ++i )
{
if ( fscanf(file,
"Number: %d "
"Species: %19s "
"Concentration: %e "
"Density: %lf "
"Molecular Weight: %lf "
"Production Rate: %lf "
"Saturation Vapor Pressure%*[^:]: C=%lf D=%lf "
"Surface Tension%*[^:]: A=%lf B=%lf ",
&mycon[i].h,
mycon[i].name,
&mycon[i].conc,
&mycon[i].rho,
&mycon[i].mw,
&mycon[i].prod,
&mycon[i].C, &mycon[i].D,
&mycon[i].A, &mycon[i].B) != 10 )
{
break;
}
}
fclose(file);
for ( j = i, i = 0; i < j; ++i )
{
printf("Number: %d\n"
"Species: %s\n"
"Concentration: %e\n"
"Density: %f\n"
"Molecular Weight: %f\n"
"Production Rate: %f\n"
"Saturation Vapor Pressure: C=%f D=%f\n"
"Surface Tension: A=%f B=%f\n\n",
mycon[i].h,
mycon[i].name,
mycon[i].conc,
mycon[i].rho,
mycon[i].mw,
mycon[i].prod,
mycon[i].C, mycon[i].D,
mycon[i].A, mycon[i].B);
}
}
return 0;
}
Originally Posted by
NickHolmes
2) Why do you have
"Number: %d Species: %19s Concentration: %e "
and
"Density: %lf Molecular Weight: %lf Production Rate: %lf "
on separate lines. I tried it with them all within one set of quotation marks and it only read the first line. How come what is so special about your use of quotation marks
Adjacent string literals are concatenated. So "Hello " "world" becomes "Hello world". Intervening whitespace (space, tab, newline, etc.) does not mess with this.
Originally Posted by
NickHolmes
3) Finally I see that conname doesn't have a & infront of it Why? and if I want to have a name that is more than 1 character long how do I do it.
For a single character with %c you would use &conname. But for a string (%19s), you don't use &conname because an array name (conname) used in an expression decays into a pointer to the first element (&conname[0]). You don't want &conname for a string because the type is not correct.