سؤال

In below code I want to delete records by fwrite(&emptyEntry...) row. Below code works but when I try to make the entry obsolete by assigning all the struct members 0 or null like:

client.accountNo=0;
client.firstName="";
client.lastName="";
client.balance=0;

compiler gives me error by saying "expression must be lvalue". acccountNo and balance does not give any errors but firstName and lastName variable assignments underlined by the compiler. Do you know why causes this? Can I use the above code can not be used for deletion of records individually assigning struct members?

fread(&client, sizeof(struct carData), 1, filePointer);

if (client.accountNo == 0)
{
    printf("This record does not exist!!!!\n\n");

}

else
{

    fseek(filePointer, (del - 1)*sizeof(struct clientData), SEEK_SET);
    fwrite(&emptyEntry, sizeof(struct clientData), 1, filePointer);
    printf("Entry deleted.");
}
هل كانت مفيدة؟

المحلول 2

client.firstName[0] = '\0' ;
client.lastName[0] = '\0' ;

will solve your problem. That's because, the string is an array of chars in C, which is as long as it doesn't encounters a \0 character. Now, if you will make the first char = '\0', then the string would end there itself.

نصائح أخرى

Replace

client.firstName="";

by

client.firstName[0] = 0 ;

which is roughly the same as

strcpy(client.firstName[0], "") ;

Remember that, unlike in other languages, there is no such thing as a string type in C.

Probably fistName & lastName have declared as array and hence compiler is giving the error.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top