Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Reading a config file in C


ghost's Avatar
0 0

So I have been making an IRC bot in C and I decided to use a configuration file for all the settings. So the user can just edit it to his/her liking and you don't need to recompile it or anything. I have a struct with all the variables needed. Then when main() starts it passes an instance of the struct to load_config() by reference. Then this is where I am having trouble. I fill in the struct's host from the file then back in main I print it out and it does print out 127.0.0.1 but on the next line it has some weird looking chacters (this is what i compiled http://pastebin.com/fCrgJFbY). Furthermore when I tried to fill out another variable in my struct it over writes the structs host. (this prints out questions.txt for both host and location, and some additional random characters http://pastebin.com/uxH7737b) I really don't know why this is happening, so any suggestions or ideas would be very much appreciated. O and keep in mind the above code was just taken out of the main program for debugging.


ghost's Avatar
0 0

Only taken a quick look at it and off the top of my head I'd say that I see no memset(temp_buf, NULL, 100); anywhere.


ghost's Avatar
0 0

COM wrote: Only taken a quick look at it and off the top of my head I'd say that I see no memset(temp_buf, NULL, 100); anywhere.

Thanks for the reply, and good call I forgot to put that in there :angry:. Unfortunately the problem is still present but I added memset(temp_buf, '\0', 100); at the end of the file reading while() loop. (http://pastebin.com/Khr5ntH7)


ghost's Avatar
0 0

Well, you could try to copy the stuff into the struct which you've passed instead of into usr_data so that you don't just assign a pointer to something probably reused that only exists in the function anyhow.


spyware's Avatar
Banned
0 0

Wild shot in the dark here, but maybe you need to trim your input.


ghost's Avatar
0 0

spyware wrote: Wild shot in the dark here, but maybe you need to trim your input. Probably not, but even if he does, he will need to copy the contents from usr_data instead of doing what he does which is just to assign a pointer. Assigning just a pointer to something that's reused will fuck him over, especially when it's a pointer to a local variable in a separate function. Even if it would work in one instance it's a very bad habit to get in to. Heck, I'm surprised he didn't get a warning about that shit.