hi friends am reading the data from the file and delimiting the value with the help of ','. input file:
shankar,kumar,ooty
ravi,,cbe
code:
while ( fgets ( mem_buf, sizeof mem_buf, infile ) != NULL )
{
item = strtok(mem_buf,delims);
printf("1 val:%s",item);
item = strtok(NULL,delims);
printf("2 val:%s",item);
item = strtok(NULL,delims);
printf("3 val:%s",item);
}
for the above input file output like:
1:shankar 2:kumar 3:ooty
1:ravi 2:cbe 3:
but i need output like,
1:shankar 2:kumar 3:ooty
1:开发者_如何学编程ravi 2: 3:cbe
because in the input file for the second row middle data is null? could you please help me on this? i know this happens because no space between ,, in the second row of input file?please tell me any other alternative way? or i should for alter the input file before it read?
There's nothing stopping you from coding up your own strtok
-like function:
#include <stdio.h>
#include <string.h>
static char *myStrTok (char *s, int c) {
static char *nextS = NULL; // Holds modified delimiter.
static int done = 1; // Flag to indicate all done.
// Initial case.
if (s != NULL) {
// Return NULL for empty string.
if (*s == '\0') { done = 1; return NULL; }
// Find next delimiter.
nextS = s;
while ((*nextS != c) && (*nextS != '\0')) nextS++;
done = (*nextS == '\0');
*nextS = '\0';
return s;
}
// Subsequent cases.
if (done) return NULL;
// Put delimiter back and find next one.
*nextS++ = c;
s = nextS;
while ((*nextS != c) && (*nextS != '\0')) nextS++;
done = (*nextS == '\0');
*nextS = '\0';
return s;
}
static int prt (char *s) {
char *s2 = myStrTok (s, ','); printf ("1: [%s]\n", s2);
s2 = myStrTok (NULL, ','); printf ("2: [%s]\n", s2);
s2 = myStrTok (NULL, ','); printf ("3: [%s]\n", s2);
s2 = myStrTok (NULL, ','); if (s2 != NULL) printf ("4: [%s]\n", s2);
printf ("==========\n");
}
int main (void) {
char x[] = "shankar,kumar,ooty"; char y[] = "ravi,,cbe";
prt (x); prt (y);
printf ("[%s] [%s]\n", x, y);
return 0;
}
This outputs:
1: [shankar]
2: [kumar]
3: [ooty]
==========
1: [ravi]
2: []
3: [cbe]
==========
[shankar,kumar,ooty] [ravi,,cbe]
as you desire. It only handles a single character for the separator but that appears to be adequate in this case.
From the manpage: "A sequence of two or more contiguous delimiter characters in the parsed string is considered to be a single delimiter. Delimiter characters at the start or end of the string are ignored. Put another way: the tokens returned by strtok() are always nonempty strings."
So if you must use strtok, you will have to modify the input first somehow.
You could check the length of each item
to make sure it's not 0.
Better yet, combine this with looping each line (example) so that you're not limited to three values.
If strtok
cannot do the job you want, you can code up your own parser, something along the lines of:
#include <stdio.h>
#include <string.h>
static int prt (char *s) {
char *s2;
s2 = strchr (s, ','); printf ("1: [%*.*s]\n", s2-s, s2-s, s); s = s2 + 1;
s2 = strchr (s, ','); printf ("2: [%*.*s]\n", s2-s, s2-s, s); s = s2 + 1;
printf ("3: [%s]\n", s);
printf ("==========\n");
}
int main (void) {
char x[] = "shankar,kumar,ooty"; char y[] = "ravi,,cbe";
prt (x2); prt (y2);
return 0;
}
That little sample shows you the difference between a strtok
solution and a manual strchr
solution, the output being:
1: [shankar]
2: [kumar]
3: [ooty]
==========
1: [ravi]
2: []
3: [cbe]
==========
You'll need to watch out for cases where you don't have enough delimiters (or too many) but that should be a minor modification.
Here's a technique that I shamelessly copied from Rob Pike:
void print_tokens(char *s)
{
char *p, *last;
last = s;
for (p = strchr(s, ','); p; p=strchr(p+1, ',')){
*p = 0;
puts(last);
*p = ',';
last = p+1;
}
puts(last);
}
I didn't test the above code, so there may be some minor flaws. The main idea should be clear, though.
精彩评论