Just got the code to compile for day 2 of the advent of code attempt. It now prints “segmentation fault (core dumped)”😂 I used gdb for the first time and it looks like it’s happening when i call fgets().

This is cool! My program is messed up…but I have a lead.

@derickflorian Are you passing the handle of a file you opened? (It's been 20 years since I've used C, but segmentation fault means you're trying to access a memory location you can't. Also it won't allocate a string for you. Your string must be allocated first. char mystring[] won't cut it.)

Follow

@shaun_haney I think for that error I was accessing an array out of bounds. I was passing in the path specified passed by the command line by I used argv[2] when I really meant the second argument(argv[1]).

I’m going to pick it up again this weekend but my next problem is I used strok() incorrectly because I didn’t know you passed in NULL when you are trying to read the next token. I’m definitely getting all the possible errors out of the way so my code will be perfect later :).

Thank you for the help!

@shaun_haney but now that I think a little bit more, that makes sense. Fopen probably returned garbage because it wasn’t getting a valid path and then fgets was accessing some place it couldn’t maybe?

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.