Let say I have a file looks like this
51.41 52.07 52.01 51.22 50.44 49.97 Coal Diggers
77.26 78.33 78.29 78.12 77.09 75.74 Airplane Flyers
31.25 31.44 31.43 31.09 31.01 30.92 Oil Fracting and Pumping
52.03 12.02 12.04 22.00 31.98 61.97 Big Bank
44.21 44.32 44.29 43.98 43.82 43.71 Rail Container Shipping
93.21 93.11 93.02 93.31 92.98 92.89 Gold Bugs
I want to read this file word using fscanf to put the numbers in float arrays and words in an array of strings. But, after few hours of strenuous thinking, I still can't figure out how to resolve this thing.
void dataInsert (COMPANY* company1, COMPANY* company2, COMPANY* company3, COMPANY* company4, COMPANY* company5, COMPANY* company6)
{
//Function Declaration
FILE* spData;
float number;
char* name[20];
//Statement
if ((spData = fopen("dataFile","r")) == NULL)
{
fprintf(stderr, "ERROR OPENING!!");
exit (1);
}
int i = 0;
int numCount = 0;
int lineCount = 0;
while (fscanf(spData, "%f", &number) != EOF)
{
if(isdigit(number))
{
if (lineCount == 0)
{
company1 -> stock_price[i] = number;
}
else if (lineCount == 1)
{
company2 -> stock_price[i] = number;
}
else if (lineCount == 2)
{
company3 -> stock_price[i] = number;
}
else if (lineCount == 3)
{
company4 -> stock_price[i] = number;
}
else if (lineCount == 4)
{
company5 -> stock_price[i] = number;
}
else if (lineCount == 5)
{
company6 -> stock_price[i] = number;
}
numCount++;
i++;
if (numCount == 6)
{
lineCount++;
numCount = 0;
i = 0;
}
}
}//while
fclose (spData);
}//dataInsert
I don't know what to do with strings at the end of each line. I want to put those string in structure company -> name[10]. Those data are in a text file.
<br>tags in the file? Is this file XML or HTML or something? Maybe you should use a parsing library? If the file is exactly the format you show here, you can parse it with C, but a scripting language would be easier... Python would be my choice.