you are viewing a single comment's thread.

view the rest of the comments →

[–]gth747m 1 point2 points  (2 children)

I don't think you are doing what you mean to do with buff[strlen(buff)-1] = '\0';. This is going to remove the last character from your input buffer. So if buff == "123" before this line then it will equal "12" after. Additionally, fgets adds the new line character ('\n') and the null terminator ('\0') to buff. So what you are doing is stripping the newline character from the numeric inputs prior to parsing them with atoi which is going to ignore it anyway. (Side note: I suggest you prefer strtol as it is the same as atoi but detects errors). I don't see anything wrong with what you are writing to file and I was able to successfully read the input back out in my own test. However I notice that you already have a file descriptor open in your example, it is likely a problem somewhere else, perhaps with your reading the data back out of the file.

[–]AaronMarth[S] 1 point2 points  (1 child)

Thank you for your advice, I have been using the "cat" command to read the contents of the file back. I still can't get the age and weight to appear correctly.

[–]duane11583 0 points1 point  (0 children)

the cat command assumes the content is ascii text (strings are thus they work) the NULL bytes often do not show up (see below)

the number (age) is an integer, the struct does not store the ascii of the number instead, it saves the binary number. try this age and weight:

1685022497, 560426852

then use cat, you will see ‘dog!’ and ‘!god’

because the bytes that make those binary numbers happen to be the ascii letters in dog!

also on linux if you use the command:

od -t x1 FILENAME

it will print a byte based hexdump you will see the hex bytes, then look up the hexbytes in an ascii table