I have a text file in this format:
#Name #Company #ID
Name1 Brand1 1234
Name2 Brand2 5678
And a struct:
struct MyStruct{
char name[TEXT_LEN];
char company[TEXT_LEN];
char ID[TEXT_LEN];
};
typedef struct MyStruct Data_t;
What I want... is to load the data from text file in appropriate variables. It should ignore first line because those are headers...
This is what I've been trying so far but without success:
int loadDataFromFile(Data_t *items, long len, char *inputFile)
{
FILE *fp;
if ((fp = fopen(inputFile, "r")))
{
for (long i = 0; i < len; i++)
{
/*fscanf(fp, "%s %s %s\n", items[i].name, items[i].company, items[i].ID);*/
//this is commented part was not working part... More about this in EDIT
fscanf(fp, "%s %s %s\n", items[i].name, items[i].company, items[i].ID);
printf("Name: %s", items[i].name); // For first item it prints:
// Name: #Name
}
}
return 0;
}
What is wrong here?
EDIT
I've replaced the code but now it reads first line as well. Is there a way to ignore header of file (first line)?