How would I be able to write information into a randomly accessed file using data stored in a struct? I am just trying to write a program that will create a file and store test data that will be used later. It seems to work when the user enters the input and I use fscanf with stdin and getting info for each member. Why won't it work when i just try to assign each member a specific value? The int seems to work, but the string is not. This is going to be a randomly accessed file so I can not use fprintf to load the test data because i will not be able to fseek it in a different program and use the struct size/byte locations as reference. If i comment out the string lines, it will compile and save the first set of according to size. I get 2 of the same errors (both are commented below).
p.s. this is most of code, not all.
Code:
struct testinfo {
int checknum;
char payto[25];
double amount;
char description[35];
};
int main(void)
{
FILE *filePtr;
struct testinfo info = {0, "", 0.0, ""};
int i;
if ((filePtr = fopen("list.dat", "w")) == NULL)
printf("File could not be opened.\n");
else {
for (i = 0; i <= 100; i++)
fwrite(&info, sizeof(struct testinfo), 1, filePtr);
info.checknum = 100;
info.payto = "Florida Power"; // '=' : left operand must be l-value
info.amount = 125.00;
info.description = "Electricity"; // '=' : left operand must be l-value
fseek(filePtr, (info.checknum - 100) * sizeof(struct testinfo), SEEK_SET);
fwrite(&info, sizeof(struct testinfo), 1, filePtr);
Thanks in advanced.