Hi everyone.
I'm working with a comma-delimited text file (in this example, it's abc.txt), with the following structure:
Code:
299, 26, 10, 7, 45, 87.688493, 112.055298, 54697, 3362, 1992, 2569, 793, 81638, 3
299, 26, 10, 8, 0, 85.539068, 114.966376, 49899, 5755, 3751, 3014, 994, 89688, 5
299, 26, 10, 8, 15, 83.402953, 117.925606, 47183, 7941, 4041, 3164, 1084, 93264, 5
I'd like to parse this code into fourteen separate arrays, so each 'column' is in its own array. For example, the output for the first four items of each line should be in this structure:
Code:
array1[299,299,299]
array2[26,26,26]
array3[10,10,10]
array4[7,8,8]
I'm incorporating this code into a program that can read from different user-selected text files which have different numbers of lines, but will always the same fourteen-variable structure. From what I've gathered, I'll need to use a While loop with fgets inside of it to accomplish this.
I've tried several different methods to get the data into the arrays, such as strtok, sccanf, and a combination of the two. Everything I've ended up with was a bunch of garbage numbers, or blank space.
Please forgive any noobie mistakes I may have made. This is my first outing with C programming. Here's the code I've worked with so far:
This first attempt I made at the code produces garbage numbers.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char line[200];
int i;
int aJulianDay[50];
int aDay[50];
int aMonth[50];
int aHour[50];
int aMinute[50];
float a1[50];
float a2[50];
int a3[50];
int a4[50];
int a5[50];
int a6[50];
int a7[50];
int a8[50];
int aHazardRating[50];
FILE *pFile;
pFile = fopen("c:\abc.txt", "rt");
// Load each different 'column' of values into an array
//fgets gets a string from a file
//one string per line
while(fgets(line, sizeof line, pFile) != NULL)
{
// sscanf reads the data from the strings and stores it in arrays
// since the line is in a string, it finds each of the values and stores
// them into the specified arrays. The loop does this for each line
// so, each julian day etc will be read into array aJulianDay[] each time there's a line
// Thus each variable is stored into a different array
if(sscanf(line, "%d,%d,%d,%d,%d,%lf,%lf,%d,%d,%d,%d,%d,%d,%d", &aJulianDay[i], &aDay[i], &aMonth[i], &aHour[i], &aMinute[i], &a1[i], &a2[i], &a3[i], &a4[i], &a5[i], &a6[i], &a7[i], &a8[i], &aHazardRating[i]) == 14)
{
++i;
}
}
//Close the file
fclose(pFile);
//Check the 2nd element of each array to see if it worked
printf("%d\n",aDay[1]);
printf("%d\n",aMonth[1]);
printf("%d\n",aHour[1]);
printf("%d\n",aMinute[1]);
printf("%d\n",a1[1]);
printf("%d\n",a2[1]);
printf("%d\n",a3[1]);
printf("%d\n",a4[1]);
printf("%d\n",a5[1]);
printf("%d\n",a6[1]);
printf("%d\n",a7[1]);
printf("%d\n",a8[1]);
printf("%d\n",aHazardRating[1]);
getch();
return 0;
}
I scrapped that and took a different approach, using strtok to try to parse the files using the comma delimiters. I got as far as this:
Code:
//Working copy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *pFile;
pFile = fopen("c:\abc.txt", "r");
//fgets gets a string from a file
//one string per line
fgets(line, sizeof line, pFile);
char delimiter[] = ",";
char *token;
int i5 = 0;
int input[15];
token = strtok(line, delimiter);
while (token != NULL)
{
sscanf(token, "%d", &JulianDay1);
input[i5++] = JulianDay1[i5];
token = strtok(NULL, delimiter);
}
//Close the file
fclose(pFile);
//Test to see if elements are correct
printf("%d\n",JulianDay1[0]);
printf("%d\n",JulianDay1[1]);
printf("%d\n",JulianDay1[2]);
printf("%d\n",JulianDay1[3]);
printf("%d\n",JulianDay1[5]);
printf("%d\n",JulianDay1[7]);
printf("%d\n",JulianDay1[8]);
printf("%d\n",JulianDay1[9]);
printf("%d\n",JulianDay1[10]);
printf("%d\n",JulianDay1[11]);
printf("%d\n",JulianDay1[12]);
printf("%d\n",JulianDay1[13]);
printf("%d\n",JulianDay1[13]);
getch();
getch(); //See exactly what you're doing
return 0;
}
But, that didn't work either. So, I tried another approach:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char line[300];
char delimiter[] = ",";
char *token;
int var;
int input[15];
int i = 0;
FILE *pFile;
pFile = fopen("c:\abc.txt", "r"); //Open that file in Read mode
while(fgets(line, sizeof line, pFile)) //Read the file one line at a time until the EOF
{
token = strtok (line, delimiter); //Pull the string apart into tokens using the commas
while (token != NULL)
{
sscanf (token, "%d", &var); //Scan that token into your placeholder
input[i++] = var;
printf("%d/n", input[i]); //Print out the array
token = strtok (NULL, delimiter);
}
}
fclose(pFile); //Close that file
getch(); //See exactly what you're doing
return 0;
}
That didn't work either.
My head is spinning at this point, and I'm not sure which one of these methods I'm closest to getting any kind of valid output from. I would really appreciate any help in getting these fourteen arrays populated with my data. I've tried every avenue I've found, but I can't quite figure it out. Sorry in advance for any newbie mistakes I may have made.