So I'm trying to open a textfile, which looks something like this:
name 1.52
someothername 2.42
...and so on
Now I have to copy these strings and floats into arrays, so I can mess around with them later in my code. The thing is, I'm having trouble assigning the strings that are being found in the textfile my array that I need somewhere else in my code. The arrays have the space 100, because this is supposed to be the maximum length that I allow. I know that the problem lies here
char a[100];
and here
fscanf(fp, "%s %f\n", &a, &b);
how can I pass my empty array to the fscanf function properly so that it can be "filled"??
I keep getting errors like
"warning: format specifies type 'char ' but the argument has type 'char ()[100]' [-Wformat]"
/* reads the list into the arrays name and prices and returns the number of lines (i.e.
elements) that have been read */
int read_priceList(const char* path, char names[100][100],
float price[100]){
FILE *fp = fopen(<path>, "r");
char a[100];
float b;
int i = 0;
if(fp == NULL) {
perror("Error opening File");
return -1;
}
while (!feof(fp) && i < 100) {
fscanf(fp, "%s %f\n", &a, &b);
strcpy(names[i], &a);
price [i] = b;
i++;
}
i--;
fclose(fp);
return i;
}
Also later in my code I want to pass an empty, two dimensional string array to this function, which is also a problem...
warning: incompatible pointer types passing 'char ' to parameter of type 'char ()[100]
First, I initialize them (this now happens in the main function, which calls the read_pricelist function)
char namesPricelist[100][100];
float prices[100];
and then I want to call the readPricelist function and give them these parameters.
readPricelist(<path>, &namespricelist[0][0], &prices[0]);
I'm starting to lose my mind, please help...