#Trying to fread and fwrite to a file in C
25 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
@rough comet
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
This is my code thus far:
In contrast to the sample here, my positions seem to be wrong, not sure what im doing wrong :(
Trying to fread and fwrite to a file in C

wrong role, mb
is there a c helper role
out of interest: which one did you ping? 😛
You should just stack allocate your strings in the createContacts function, since you don't need that memory outside of the scope and it's a constant buffer size.
Why do you expect choice to contain anything meaningful here? You declare (but do not initialize) it right before you compare it to "Yes"
Looks like you write each string (including the null byte) individually and then you write a struct containing each position in the file for the values?
Then you set the write position to be behind the information from the struct and write it again, overwriting some of the strings you wrote to the file
In readContacts you fread the size of struct contact expecting to get something meaningful when you had written the strings there first
Since none of these strings will have a space in them, I would just store these as space separated values on each line. To retrieve them later, read in the entire line with fgets and split it up on the spaces with strtok, strtok_r, or sscanf
I would bet they tried to ping @Chelp and it tabbed in as @Chelp
You could even use fscanf directly on the file I suppose
tldr
finding the size of each member, id, string first or offset for that matter, and then scanning using fgets made a difference
as well as finding the structs and all its members sizes as wlel
b4 fwirting
to then get the correct positions