Im writing a simple program that will write data into a random access file from a structure containing the data.
im having a bit of an issue with the fwrite function and have read the man page and
still having no luck. Im thinking it may be my logic.
here is the function that does all the nifty stuff.
Code:
void addaccount(void)
{
FILE *datafile;
ACCOUNT newacct;
char grabed[40];
int tempdata;
datafile = fopen("data.dat","w");
if (datafile == NULL)
printf("Cannot open file for write. May be a permissions issue.\n");
else
{
printf("Please enter the account number to add\n:>");
scanf("%s",grabed);
sscanf(grabed,"%d",&tempdata);
newacct.accnum = tempdata;
printf("\nPlease enter the account holders first and last name\n:>");
scanf("%s%s",newacct.first,newacct.last);
printf("\n");
fwrite(newacct,sizeof(ACCOUNT),1,datafile);
printf("Account information written to file\n");
}
}
from what I gather fwrite requires there be a const ptr used to write to the stream pointed by FILE * . I thought when you create a structure AKA intilaize it what it does is make the name you give it a pointer to the structure data in memory. but gcc is telling me its incompatible. Now im probably wrong with my implementation here but i was sure there was a way to do this without having to use malloc and create a void pointer to sizeof(struct name) or is that how you do it and im just following a red herring ?