Bug in RTL sscanf and strtok

G

Guest

I tried to submit this as a bug, but the web page failed to work. I am using visual C++ 6 SP5. I am running on a Window ME machine with 128 megs, gigs of disk, PentiumII 400. I was developing code to parse text delimited by commas. It worked fine as long as none of the fields were empty, i.e. text,text,text worked, but text,,text did not. First I tried it with sscanf then with strtok. When I built the code below in a debug build it crashed when run with an exception in strtok

startcode------------------------
#include "stdafx.h
#include <stdio.h
#include <string.h
#include <stdlib.h
#include <conio.h

int main(int argc, char* argv[]


char a[200], b[20], c[20], *p

sscanf("test,,test2", "%19[^,],%19[^,],%s", a, b, c )
printf( "a=\'%s\' should be \'test\', b=\'%s\' should be \'\', c=\'%s\' should be \'test2\'\n", a, b, c )
p=strtok("test,,test2",",")
printf( "tok=\'%s\' should be \'test\'\n", p )
p=strtok(NULL,"," )
printf( "tok=\'%s\' should be \'\'\n", p )
p=strtok(NULL,",")
if ( p==NULL
printf( "tok was NULL, should be \'test2\'\n" )
els
printf( "tok=\'%s\', should be \'test2\'\n", p )
getche()
return 0


endcode---------------------------
 
J

Jon

Think of the delimiter list as a white space list. There is no such thing as an "Empty field". An 'Empty field' is just some white
space to be skipped.


CJNoyes said:
I tried to submit this as a bug, but the web page failed to work. I am using visual C++ 6 SP5. I am running on a Window ME machine
with 128 megs, gigs of disk, PentiumII 400. I was developing code to parse text delimited by commas. It worked fine as long as none
of the fields were empty, i.e. text,text,text worked, but text,,text did not. First I tried it with sscanf then with strtok. When I
built the code below in a debug build it crashed when run with an exception in strtok.
startcode-------------------------
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

int main(int argc, char* argv[])

{
char a[200], b[20], c[20], *p;

sscanf("test,,test2", "%19[^,],%19[^,],%s", a, b, c );
printf( "a=\'%s\' should be \'test\', b=\'%s\' should be \'\', c=\'%s\' should be \'test2\'\n", a, b, c );
p=strtok("test,,test2",",");
printf( "tok=\'%s\' should be \'test\'\n", p );
p=strtok(NULL,"," );
printf( "tok=\'%s\' should be \'\'\n", p );
p=strtok(NULL,",");
if ( p==NULL )
printf( "tok was NULL, should be \'test2\'\n" );
else
printf( "tok=\'%s\', should be \'test2\'\n", p );
getche();
return 0;
}

endcode---------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top