0

I have created a linkedlist. Sent the linkedlist to a file with a write and read option. I am stuck with with load function. I am also 2 months new to C and a noob to this site.

My Code:

void load( char filename[10], struct node *np){
    // creating a temporary variable for holding a record 
    char tmpfirstName[30];
    char tmplastName[30];
    char tmpPhoneNo[15];
    char tmpeMail[55];
    char tmpAddress[255];
    int counter;

    // declare a file pointer
    FILE *input= fopen(filename, "r+");

    //check if the file opened successfully
    if (input==NULL)
        perror ("Error opening file");
    else{
        counter=0; 
        // continue in reading the file till the EOF
        while(!feof(input)){
            strcpy(tmpfirstName," ");
            fscanf(input,  "%s %s %s %s %s -=", tmpfirstName, tmplastName, tmpPhoneNo, tmpeMail, tmpAddress);
            if(strcmp(tmpfirstName," ")!=0){
                strcpy(np->[counter].firstName, tmpfirstName);
                strcpy(np->[counter].lastName, tmplastName);
                strcpy(np->[counter].phoneNo, tmpPhoneNo);
                strcpy(np->[counter].eMail, tmpeMail);
                strcpy(np->[counter].address, tmpAddress);
                counter++;
            }
        }
        fclose(input);
    }

The errors I am getting:

C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(134): error #2047: Expected a field name.
C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(135): error #2047: Expected a field name.
C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(136): error #2047: Expected a field name.
C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(137): error #2047: Expected a field name.
C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(138): error #2047: Expected a field name.
Aakash Jain
  • 1,963
  • 17
  • 29
JasonL1983
  • 13
  • 4
  • 1
    Please note that [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler Mar 26 '15 at 05:54
  • 1
    You've not identified lines 134-138, but presumably the range includes the line `strcpy(np->[counter].firstName, tmpfirstName);` as line 134. The notation `np->[counter].firstName` is erroneous. Given the declaration in the function parameter list, you need `np[counter].firstName`. – Jonathan Leffler Mar 26 '15 at 05:57
  • you would want to move fclose inside the else statement checking whether file was opened or not. – AndersK Mar 26 '15 at 06:13

3 Answers3

0

You have to use the array of structure or else use the pointer for loading data to the structure.

You try it like this ..

        strcpy(np[counter].firstName, tmpfirstName);
        strcpy(np[counter].lastName, tmplastName);
        strcpy(np[counter].phoneNo, tmpPhoneNo);
        strcpy(np[counter].eMail, tmpeMail);
        strcpy(np[counter].address, tmpAddress);
        counter++;
Esakki Thangam
  • 355
  • 2
  • 7
0

You've not identified lines 134-138, but presumably the range includes the line

strcpy(np->[counter].firstName, tmpfirstName);

as line 134. The notation np->[counter].firstName is erroneous. Given the declaration in the function parameter list, you need np[counter].firstName.

Please note that while (!feof(file)) is always wrong.

You should be testing the I/O operation directly, for example using:

    while (fscanf(input, "%29s %29s %14s %54s %254s -=", tmpfirstName, tmplastName,
                  tmpPhoneNo, tmpeMail, tmpAddress) == 5)
    {
        strcpy(np[counter].firstName, tmpfirstName);
        strcpy(np[counter].lastName, tmplastName);
        strcpy(np[counter].phoneNo, tmpPhoneNo);
        strcpy(np[counter].eMail, tmpeMail);
        strcpy(np[counter].address, tmpAddress);
        counter++;
    }

The numbers prevent the data overflowing your character arrays.

Note that most addresses have blanks in them and scanning a string with %s (with or without the size limit) will skip leading blanks, collect non-blanks in the variable, but stop at the first blank. You might need to use a scan-set: %254[^=] for example. You will never know with either the original code or the revised code whether the -= at the end of the format string was matched or not.

I note that you will eventually need to upgrade the function so that you tell it how big the array is, so that it does not overflow the bounds of the array. You would add a check before copying the data into the np array.

You should probably also upgrade the function so that it returns a success/failure indication, and probably a mechanism that indicates how many records were read into the array. One common way to do that might be to have the function return an integer, using EOF (a negative number, normally -1) to indicate problems, and a zero or a positive number to indicate how many records were read successfully.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank you so much Jonathan. I was reading the article you posted on while(!feof(file)) was wrong but I was really understanding why. Your explanation cleared up a lot of things with the file. – JasonL1983 Mar 26 '15 at 17:22
0

Also, note that this is not how a linked list works. After reading from the file, you will need to create a node, put the data into the node, and then link it to the previous handler. The following is a brief idea ...

Assuming you have

struct node *temp;

declared earlier, you need to recreate everything within your loop

temp = (struct node*)malloc( sizeof( struct node ) );
strcpy(temp->firstName, tmpfirstName );
/* copy the rest of the stuff here */
temp->next = NULL;

if (np == NULL) np = temp;
else  {
    np -> next = temp;
    np = np -> next;
}
ssm
  • 5,277
  • 1
  • 24
  • 42
  • I have a function doing this. Do I still need to have this in the load() function? wouldn't that be redundant? sorry as I said I am new to C. – JasonL1983 Mar 26 '15 at 17:34
  • You need to think about *where you want to place the data* once you read from the file. Even if you have a function that creates a liked list earlier, when you are reading the data from a file, you are essentially *recreating* the entire linked list in memory from the disk. So you will still have to do this, – ssm Mar 30 '15 at 04:12