#Trying to fread and fwrite to a file in C

25 messages · Page 1 of 1 (latest)

rough comet
#

This is what im trying to achieve, but im having troulbe freading and fwriting correctly, with the correct positions as well

wicked warrenBOT
#

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

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

rough comet
rough comet
#

Trying to fread and fwrite to a file in C

quasi sapphire
rough comet
quasi sapphire
#

out of interest: which one did you ping? 😛

rough comet
#

my code broke

#

so im broken

strange condor
#

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

strange condor
strange condor
rough comet
#

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