Join us at IRC!
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill
Wednesday, February 08, 2012
Navigation
Members Online
Total Online: 45
Web Spiders: 20
Guests Online: 40
Members Online: 5

Registered Members: 67250
Newest Member: fabek
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Reading a config file in C

skathgh420
Member



Posts: 418
Location: 127.0.0.1
Joined: 03.03.08
Rank:
God
Posted on 09-03-10 19:08
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.


1 3 3 www.google.com
Author

RE: Reading a config file in C

COM
Banned



Posts: 800
Location:
Joined: 31.08.07
Rank:
God
Posted on 09-03-10 20:48
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.


K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth!
Author

RE: Reading a config file in C

skathgh420
Member



Posts: 418
Location: 127.0.0.1
Joined: 03.03.08
Rank:
God
Posted on 09-03-10 21:39
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)


1 3 3 www.google.com
Author

RE: Reading a config file in C

COM
Banned



Posts: 800
Location:
Joined: 31.08.07
Rank:
God
Posted on 09-03-10 22:04
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.


K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth!
Author

RE: Reading a config file in C

spyware
Member



Posts: 4158
Location: The Netherlands
Joined: 14.04.07
Rank:
God
Warn Level: 90
Posted on 09-03-10 22:23
Wild shot in the dark here, but maybe you need to trim your input.


The most censored HBH profile.

"The chowner of property." - Zeph
Widespread intellectual and moral docility may be convenient for leaders in the short term,
but it is suicidal for nations in the long term.
- Carl Sagan
“Since the grid is inescapable, what were the earlier lasers about? Does the corridor have a sense of humor?” - Ebert
http://bitsofspy.net
Author

RE: Reading a config file in C

COM
Banned



Posts: 800
Location:
Joined: 31.08.07
Rank:
God
Posted on 09-03-10 22:47
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.


K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth!
Author

RE: Reading a config file in C

skathgh420
Member



Posts: 418
Location: 127.0.0.1
Joined: 03.03.08
Rank:
God
Posted on 09-03-10 23:30
Thanks for the help :D I got it working now.

http://pastebin.com/jS3jGcMT

Thanks again.


1 3 3 www.google.com
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

160 x 600
Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2004 - 2012. Since 3rd December 2004.