Please Help Reading Data From A File
Hi everybody,
I have an input file called cond.inp
The file contains data in the form as follows:
Number:1 Species: first Concentration: 1.0e14 Density: 4.00 Molecular Weight: 1.0 Production Rate: 1.0 Saturation Vapor Pressure of material in the form Ps=exp(C-D/T)P: C=1.0 D=1.0 Surface Tension in the form A-BT(dyne/cm): A=1.0 B=0.0
Number:2 Species: second Concentration: 1.0e14 Density: 2.00 Molecular Weight: 1.0 Production Rate: 1.0 Saturation Vapor Pressure of material in the form Ps=exp(C-D/T)P: C=1.0 D=1.0 Surface Tension in the form A-BT(dyne/cm): A=1.0 B=0.0
Number:3 Species: third Concentration: 1.0e14 Density: 1.00 Molecular Weight: 1.0 Production Rate: 1.0 Saturation Vapor Pressure of material in the form Ps=exp(C-D/T)P: C=1.0 D=1.0 Surface Tension in the form A-BT(dyne/cm): A=1.0 B=0.0
Number:4 Species: fourth Concentration: 1.0e14 Density: 4.00 Molecular Weight: 1.0 Production Rate: 1.0 Saturation Vapor Pressure of material in the form Ps=exp(C-D/T)P: C=1.0 D=1.0 Surface Tension in the form A-BT(dyne/cm): A=1.0 B=0.0
As you can see there are a a number of lines that contains a series of names with values. Each line has the same format, although the length of the Species value is not set.
Could some please tell me how I can read the data into a series of arrays.
I have the following code and it works to some degree:
Code:
//part of program that opens the required file
FILE *fptr1;
if((fptr1=fopen("cond.inp","r"))==NULL)//Opening the input file that contains property data for the aerosol material.
{
printf("Warning: Could not open file");
return(-1);
}
//Scanning the property data from the input file "cond.inp"
fscanf(fptr1,"Number: %d Species: %c Concentration: %e Density: %lf Molecular Weight: %lf Production Rate: %lf Saturation Vapor Pressure: C=%lf D=%lf Surface Tension: A=%lf B=%lf ",&h,&conname[1],&conconc[1],&conrho[1],&conmw[1],&conprod[1],&C[1],&D[1],&A[1],&B[1]);
fclose(fptr1);//Closing the main property data file.
The code works for reading the first line of data. However I have tried to read the subsequent data in by repeating the Fscan statement seven times but the subsequent data values were not read by the code.
Can I set up a loop to be repeated x number of times to read in the subsequent lines? (I will have to replace [1] by[h], where h is a number that changes from 1-7.
Also how do I read in the Species: as a series of characters (ie so the values first, second third etc are read not f, s, t etc)
Please help I am so sorry but I have searched through the faq and previous threads as well as the web but have not found an answer to my problem.
Many thanks,
Nick
Thanks Dave but a few questions
Dave,
That works great. However a few questions.
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.
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
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.
Thanks alot for the first solution.
All the best,
Nick
PS Sorry if the questions are stupid but it is amazing how some very small differences make such huge differences