-2

I'm getting a strange error and I'm not sure why. I am attempted to scan from a txt file to a struct array and I am getting an error. It expects a ']' after SIZE even though I have a closing bracket for the array length.

Heres the snippet of code that the error is happening in. I am brand new to learning structures so if anything is wrong other than the initial question, please let me know.

Here is the struct definition:

struct employData{
char first[7];
char initial[1];
char last[9];
char street[16];
char city[11];
char state[2];
char zip[5];
int age;
char sex[1];
int tenure;
double salary;

};

and then here is the scan function that is not working:

int readData(employData){
int i = 0;
struct employData dataArray[SIZE];
fp = fopen("payfile.txt", "r");

if (fp != NULL){
    printf("File opened. Scanning...");
    while (!(feof(fp))){
        fp = fscanf(fp, "%s %s %s %s %s %s %s %s %d %s %d %lf", dataArray[i].first, dataArray[i].initial, dataArray[i].last, dataArray[i].street, dataArray[i].city, dataArray[i].first, dataArray[i].state, dataArray[i].zip, dataArray[i].age, dataArray[i].sex, dataArray[i].tenure, dataArray[i].salary);
        i++;
    }
}
else {
    printf("File open failed.");
}
}

Thanks!

Edit: Fixed blatant error. Still having intellisense error

Pahjay
  • 17
  • 6
  • Does it compile? Does it work as expected? Maybe "intelli" be not so intelligent. Sometimes it happens. 1st make it compile, then take care of warnings. – luk32 May 01 '14 at 19:57
  • What is the definition of `SIZE`? – Jongware May 01 '14 at 19:57
  • I fixed all the errors in `fscanf` and couldn't reproduce the error you're complaining about. It's probably coming from somewhere else in your code -- probably a missing semicolon. – Barmar May 01 '14 at 19:58
  • 1
    You know, In C you can split lines in the same places as you can put a space. That can help with readability... – rodrigo May 01 '14 at 19:59
  • What is the purpose of the `employData` argument? You're not using it for anything. You also haven't declared a type for it. It's probably a bad idea to use the same name for a variable and a structure type, it gets confusing. – Barmar May 01 '14 at 20:00
  • [`while(!feof(f))` is **ALWAYS WRONG!**](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – The Paramagnetic Croissant May 01 '14 at 20:03

2 Answers2

3

You've got %c in your fscanf, which are for single characters, not strings as you seem to have.

Use %s for strings instead.

Edit: as @luk32 pointed out to me, fscanf needs file pointer as first place parametre.

fscanf = (fp, ...);

Source: http://www.manpagez.com/man/3/fscanf/

AntonH
  • 6,359
  • 2
  • 30
  • 40
  • 2
    Shouldn't `fscanf()` have a file pointer as 1st argument also? =P People are so fast to up-vote incorrect answers lol =) – luk32 May 01 '14 at 19:54
  • @luk32 Yes, missed that. Editing now. Also, I was quick to miss that mistake :) – AntonH May 01 '14 at 19:54
  • Oops, yeah. Dumb mistake on both regards, but I still can't seem to figure out why I am getting the intellisense error regarding wanting the extra ']'? – Pahjay May 01 '14 at 19:55
  • Because it's not looking for a pointer, but a single `char`. You provide a pointer to char array, not a `char` which would be indicated by which element of the array you want the char to be placed, which would require `[]`. – AntonH May 01 '14 at 19:56
1

I'm goint to guess here...

You have somewhere a line such as this one:

#define SIZE 1000;

So the definition of the struct array will expand from:

struct employData dataArray[SIZE];

to:

struct employData dataArray[1000;];

And now the error is obvious.

The solution is easy: remove the semicolon from the macro definition:

#define SIZE 1000
rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Yup, that fixed it. Learning programming really showed me my ability at making dumb mistakes. Thanks! – Pahjay May 01 '14 at 20:04