but that 2nd call of puts results in a segfault. I thought strcpy only copies. wtf is going on? I couldn't recreate it though on its own so I think it must be this project.
If you guys have nothing to do, maybe you can have a look. http://pastebin.ca/1915922
It's part of a larger project but this one here reads two lines of text from a given file and separates the tokens that are separated by commas and spaces.
>>4 As if the 2nd line that was read, b, just disappears after that last strtok. Because it does? strtok changes strings. But I don't think that that's the reason for the segfault, because it only happens at the second puts.
Or the bug is in the middle line, where string1 becomes some sort of an invalid pointer due to the use of strtok, and string2 gets overwritten with some random garbage. Or maybe you should have ensured that all your strings are 0-terminated? Like reading only 95 characters and manually setting the 96th to 0. Also, you should say char a[96]; since you already know how much memory you want to allocate.
man, I'm stumped. I remember though that it used to work. My old code made use of a loop but then I was having trouble and I realized that since there will always be 6 items per line, I might as well just call strtok 6 times manually.
It would actually make it all the way to parsing the 2nd line that was read but with last string of the 2nd line replacing 1st string of the 1st line for some reason.
I also had variables initializations left over from the loop and when I removed those (since they weren't being used) that's when shit stopped working.
Two strings walk into a bar and sit down. The bartender says, “So what’ll it be?”
The first string says, “I think I’ll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu” “Please excuse my friend,” the second string says. “He isn’t null-terminated.”
>>25 Xarn raped and killed her. Not necessarily in that order.
Name:
Anonymous2010-08-12 20:48
You're using fixed size buffers, but not making sure that unknown input fits inside them. Don't do that. Use snprintf instead of strcpy, then you at least truncate overly long input instead of overflowing buffers.
Name:
Anonymous2010-08-12 21:02
>>27
Or use "%.25s" or whatever length in the format specifiers.
Name:
Anonymous2010-08-12 22:14
>>28
There's lots of ways to do it, but snprintf is straightforward and doesn't need yet another magic number that's just begging to become a bug later.
>>30
If the buffer is an array, you can simply write sizeof s. If the buffer is a pointer, you should always have its size available via some variable or macro.
Name:
Anonymous2010-08-13 7:08
Okay, I got around my first problem by forgetting about multidimensional arrays for strings and just making multiple arrays. Inelegant but fuck it, it works.
I have a new problem now though. But it seems to be similar to the first one. I have a while loop that parses a string for numbers and it goes
I do this multiple times but for one of them, consistently the one for stats[4], the value of i jumps to the value of counter between the two printfs. What the shit again? And thanks for the help so far.
Name:
Anonymous2010-08-13 7:11
>char x[4][32];
>strcpy(x[4],pch); //Here's where it fucking happens.
Here you go
Name:
Anonymous2010-08-13 7:14
okay, wtf, I seemed to have fixed it by increasing the size of the stats array even though it really doesn't need it. Well whatever.