| | |
String splitter
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I tried writing a string splitter for my little operating system based on http://www.daniweb.com/code/snippet318.html but it didn't quite work. Here's my code:
Then I call it like this:
But instead of the "meow", "moo", "baa", "something" I was expecting, I got this instead.
Any ideas on this?
C Syntax (Toggle Plain Text)
// BTW, this will eventually return a string containing the token_numth token. But I'm just trying to get it to work for now. void strsplit(const char *str, char splitchar, int token_num) { char *delimeter = "\0"; delimeter[0] = splitchar; int i; strcpy(str, line); char *token = line; /* point to the beginning of the line */ for (i = 0; *token; i++) /* loop through each character on the line */ { /* search for delimiters */ size_t len = strcspn(token, delimeter); token[len] = '\0'; /* print the found text: use *.* in format to specify a maximum print size */ printf("token[%2d] = \"%s\"\n", i, token); /* advance pointer by one more than the length of the found text */ token += len + 1; } }
Then I call it like this:
C Syntax (Toggle Plain Text)
printf("strsplit test: /meow/moo/baa/something\n"); strsplit("/meow/moo/baa/something", '/', 0);
But instead of the "meow", "moo", "baa", "something" I was expecting, I got this instead.
•
•
•
•
Popcorn 0.3 booting...
Build 60.
Looking for preloaded modules... none found
strsplit test: /meow/moo/baa/something
token[ 0] = ""
token[ 1] = "meow"
token[ 2] = "moo"
token[ 3] = "baa"
token[ 4] = "som"
token[ 5] = "thi"
token[ 6] = "g"
// BTW, this will eventually return a string containing the token_numth token. But I'm just trying to get it to work for now.
void strsplit(const char *str, char splitchar, int token_num)
{
char *delimeter = "\0";
delimeter[0] = splitchar;
^^^ delimiter is a ponter that oritinally points to someplace
in read-only memory. You can't, in most cases, write to read-only memory like that.
When you do, either you will get runtime errors or undefined behavior
int i;
strcpy(str, line);
^^^ attempt to copy something to read-only memory. Note that your program passed a
string literal to this function, and all (or most) string literals reside in read-only memory
char *token = line; /* point to the beginning of the line */
^^^ where is variable line defined? I don't see it anywhere
for (i = 0; *token; i++) /* loop through each character on the line */
{
/* search for delimiters */
size_t len = strcspn(token, delimeter);
token[len] = '\0';
^^^ this is identical to strtok(). You may as well have used it.
/* print the found text: use *.* in format to specify a maximum print size */
printf("token[%2d] = \"%s\"\n", i, token);
/* advance pointer by one more than the length of the found text */
token += len + 1;
}
} Last edited by Ancient Dragon; Aug 26th, 2006 at 12:35 pm.
•
•
•
•
^^^ delimiter is a ponter that oritinally points to someplace
in read-only memory. You can't, in most cases, write to read-only memory like that.
When you do, either you will get runtime errors or undefined behavior
•
•
•
•
^^^ attempt to copy something to read-only memory. Note that your program passed a
string literal to this function, and all (or most) string literals reside in read-only memory
•
•
•
•
for my little operating system
•
•
•
•
^^^ where is variable line defined? I don't see it anywhere
C Syntax (Toggle Plain Text)
char line[1024];
•
•
•
•
^^^ this is identical to strtok(). You may as well have used it.
I've found that it works perfectly if there's a trailing / (or whatever delimeter) on the end of the string. But not every filename I'm parsing is a directory. Any idea on how else I can do this?
Last edited by mmiikkee12; Aug 26th, 2006 at 12:57 pm.
Got it!
For anyone googling for this, here's the code I used:
For anyone googling for this, here's the code I used:
C Syntax (Toggle Plain Text)
char tmpbuf[1024]; char retval[256]; char *strsplit(const char *str, char splitchar, int token_num) { strcpy(str, tmpbuf); // fix a stupid bug if (tmpbuf[strlen(tmpbuf) - 1] != splitchar) { tmpbuf[strlen(tmpbuf)] = splitchar; tmpbuf[strlen(tmpbuf + 1)] = '\0'; } printf("%s\n", tmpbuf); char *delimeter = "\0"; delimeter[0] = splitchar; char *token = tmpbuf; /* point to the beginning of the line */ int i; for (i = 0; *token; i++) /* loop through each character on the line */ { /* search for delimiters */ size_t len = strcspn(token, delimeter); token[len] = '\0'; /* print the found text: use *.* in format to specify a maximum print size */ printf("token[%2d] = \"%s\"\n", i, token); if (i == token_num) { strncpy(token, retval, (len < 256? len:256)); retval[(len < 256? len:256)] = '\0'; return retval; } /* advance pointer by one more than the length of the found text */ token += len + 1; } }
Last edited by mmiikkee12; Aug 26th, 2006 at 2:34 pm. Reason: yet another bugfix
•
•
•
•
Actually, all memory is marked writable, mainly because I was too lazy to look up the bits to set for read-only memory :-) Keep in mind:
Last edited by Ancient Dragon; Aug 26th, 2006 at 2:57 pm.
![]() |
Similar Threads
- String Splitter (Java)
Other Threads in the C Forum
- Previous Thread: Formatted I/O functions
- Next Thread: Creating an auto update function in an app
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql odf open opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard string strings suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






