www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sscanf problem,

reply DMINATOR <DMINATOR_member pathlink.com> writes:
Hi

I have a little strange problem, probably I am doing something wrong.

I am writing a small parser for an 3d .OBJ format. I am using sscanf for
parsing. Most of the results are correct, except one place.

Here is the part of input text file:

g object_1
f 12/934/12 228/1/228 231/936/231
f 228/1/228 72/75/72 229/935/229
f 231/936/231 229/935/229 75/937/75
f 227/2/227 231/936/231 230/10/230
f 72/75/72 695/960/695 1346/3/1346
f 695/960/695 2/1352/2 1345/1328/1345
f 1346/3/1346 1345/1328/1345 73/938/73
f 229/935/229 1346/3/1346 233/939/233
f 654/7/654 232/6/232 75/937/75
f 233/939/233 236/4/236 654/7/654
f 75/937/75 233/939/233 654/7/654
f 644/5/644 236/4/236 233/939/233
f 73/938/73 234/1340/234 644/5/644
f 233/939/233 73/938/73 644/5/644
f 236/4/236 644/5/644 647/940/647
f 644/5/644 234/1340/234 645/859/645
f 647/940/647 645/859/645 646/234/646
f 643/8/643 647/940/647 235/1032/235
f 232/6/232 654/7/654 656/941/656
f 654/7/654 236/4/236 643/8/643
f 656/941/656 643/8/643 655/231/655
f 653/942/653 656/941/656 655/231/655
f 666/15/666 242/943/242 71/9/71
f 230/10/230 244/16/244 666/15/666
f 71/9/71 230/10/230 666/15/666
f 658/12/658 244/16/244 230/10/230
f 75/937/75 232/6/232 658/12/658
f 230/10/230 75/937/75 658/12/658
f 244/16/244 658/12/658 660/11/660


--------------------------
Here is the code I use for reading that :

// 1 Vertex   --  2 Vertex   --  3 Vertex
text.sscanf("f %d/%d/%d %d/%d/%d %d/%d/%d", 
&triangles[ arpos ].vertexes[0].vid[0] , 
&triangles[ arpos ].vertexes[0].vid[1] ,
&triangles[ arpos ].vertexes[0].vid[2] ,

&triangles[ arpos ].vertexes[1].vid[0] , 
&triangles[ arpos ].vertexes[1].vid[1] ,
&triangles[ arpos ].vertexes[1].vid[2] ,

&triangles[ arpos ].vertexes[2].vid[0] , 
&triangles[ arpos ].vertexes[2].vid[1] ,
&triangles[ arpos ].vertexes[2].vid[2] );

----
text is char[]
results are just int.

Anyway all the code is parsed pretty well, except for a few places:

12/934/12 228/1/228 231/936/231
228/1/228 72/75/72 229/935/2298 <---- 2298, should be 229 !
231/936/231 229/935/229 75/937/75
227/2/227 231/936/231 230/10/230
72/75/72 695/960/695 1346/3/1346
695/960/695 2/1352/2 1345/1328/1345
1346/3/1346 1345/1328/1345 73/938/73
229/935/229 1346/3/1346 233/939/233
654/7/654 232/6/232 75/937/75
233/939/233 236/4/236 654/7/654
75/937/75 233/939/233 654/7/654
644/5/644 236/4/236 233/939/233
73/938/73 234/1340/234 644/5/644
233/939/233 73/938/73 644/5/644
236/4/236 644/5/644 647/940/647
644/5/644 234/1340/234 645/859/645
647/940/647 645/859/645 646/234/646
643/8/643 647/940/647 235/1032/235
232/6/232 654/7/654 656/941/656
654/7/654 236/4/236 643/8/643
656/941/656 643/8/643 655/231/655
653/942/653 656/941/656 655/231/655
666/15/666 242/943/242 71/9/71
230/10/230 244/16/244 666/15/666
71/9/71 230/10/230 666/15/666
658/12/658 244/16/244 230/10/230
75/937/75 232/6/232 658/12/6583  <---- 6583, should be 658 !
230/10/230 75/937/75 658/12/658
244/16/244 658/12/658 660/11/660

So any ideas, what might be bugging here :) ?
Jul 03 2006
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"DMINATOR" <DMINATOR_member pathlink.com> wrote in message 
news:e8c4qc$2pji$1 digitaldaemon.com...

 Anyway all the code is parsed pretty well, except for a few places:

 228/1/228 72/75/72 229/935/2298 <---- 2298, should be 229 !
 75/937/75 232/6/232 658/12/6583  <---- 6583, should be 658 !

 So any ideas, what might be bugging here :) ?

Hmm. I tried duplicating the problem, but it seemed to work just fine. The only thing I can think of is that since sscanf is a C function, it expects strings to be null-terminated, and perhaps your string data might not be. So try something like sscanf(toStringz(text), "format string", &all, &the, &outputs); See if that helps it.
Jul 03 2006
prev sibling parent reply David Medlock <noone nowhere.com> writes:
DMINATOR wrote:
 Hi
 
 I have a little strange problem, probably I am doing something wrong.
 
 I am writing a small parser for an 3d .OBJ format. I am using sscanf for
 parsing. Most of the results are correct, except one place.
 
 Here is the part of input text file:
 
 g object_1
 f 12/934/12 228/1/228 231/936/231
 f 228/1/228 72/75/72 229/935/229
 f 231/936/231 229/935/229 75/937/75
 f 227/2/227 231/936/231 230/10/230
 f 72/75/72 695/960/695 1346/3/1346
 f 695/960/695 2/1352/2 1345/1328/1345
 f 1346/3/1346 1345/1328/1345 73/938/73
 f 229/935/229 1346/3/1346 233/939/233
 f 654/7/654 232/6/232 75/937/75
 f 233/939/233 236/4/236 654/7/654
 f 75/937/75 233/939/233 654/7/654
 f 644/5/644 236/4/236 233/939/233
 f 73/938/73 234/1340/234 644/5/644
 f 233/939/233 73/938/73 644/5/644
 f 236/4/236 644/5/644 647/940/647
 f 644/5/644 234/1340/234 645/859/645
 f 647/940/647 645/859/645 646/234/646
 f 643/8/643 647/940/647 235/1032/235
 f 232/6/232 654/7/654 656/941/656
 f 654/7/654 236/4/236 643/8/643
 f 656/941/656 643/8/643 655/231/655
 f 653/942/653 656/941/656 655/231/655
 f 666/15/666 242/943/242 71/9/71
 f 230/10/230 244/16/244 666/15/666
 f 71/9/71 230/10/230 666/15/666
 f 658/12/658 244/16/244 230/10/230
 f 75/937/75 232/6/232 658/12/658
 f 230/10/230 75/937/75 658/12/658
 f 244/16/244 658/12/658 660/11/660
 
 
 --------------------------
 Here is the code I use for reading that :
 

Here is my function for reading OBJ faces: alias char[] string; void read_face( string data, Group target ) { if ( target is null ) throw new Exception("Face data with no group found!" ); string[] info = split( data ); if ( info.length != 3 ) throw new Exception("Malformed face data:" ~ data ); face* f = target.addface(); void add( int which, string vi, string ci ) { f.v[which] = atoi(vi)-1; f.c[which] = atoi(ci)-1; } void add_info( int index ) { string s = info[index]; string[] arr = split( s, "/" ); if ( arr.length<2 ) throw new Exception("Malformed face data group:"~ s ); else add( index, arr[0], arr[1] ); } add_info( 0 ); add_info( 1 ); add_info( 2 ); } -DavidM
Jul 05 2006
parent reply DMINATOR <kna_int mail.ru> writes:
On Wed, 05 Jul 2006 14:22:02 +0300, David Medlock <noone nowhere.com>  =

wrote:

 DMINATOR wrote:
 Hi
  I have a little strange problem, probably I am doing something wrong=


  I am writing a small parser for an 3d .OBJ format. I am using sscanf=


 for
 parsing. Most of the results are correct, except one place.
  Here is the part of input text file:
  g object_1
 f 12/934/12 228/1/228 231/936/231
 f 228/1/228 72/75/72 229/935/229
 f 231/936/231 229/935/229 75/937/75
 f 227/2/227 231/936/231 230/10/230
 f 72/75/72 695/960/695 1346/3/1346
 f 695/960/695 2/1352/2 1345/1328/1345
 f 1346/3/1346 1345/1328/1345 73/938/73
 f 229/935/229 1346/3/1346 233/939/233
 f 654/7/654 232/6/232 75/937/75
 f 233/939/233 236/4/236 654/7/654
 f 75/937/75 233/939/233 654/7/654
 f 644/5/644 236/4/236 233/939/233
 f 73/938/73 234/1340/234 644/5/644
 f 233/939/233 73/938/73 644/5/644
 f 236/4/236 644/5/644 647/940/647
 f 644/5/644 234/1340/234 645/859/645
 f 647/940/647 645/859/645 646/234/646
 f 643/8/643 647/940/647 235/1032/235
 f 232/6/232 654/7/654 656/941/656
 f 654/7/654 236/4/236 643/8/643
 f 656/941/656 643/8/643 655/231/655
 f 653/942/653 656/941/656 655/231/655
 f 666/15/666 242/943/242 71/9/71
 f 230/10/230 244/16/244 666/15/666
 f 71/9/71 230/10/230 666/15/666
 f 658/12/658 244/16/244 230/10/230
 f 75/937/75 232/6/232 658/12/658
 f 230/10/230 75/937/75 658/12/658
 f 244/16/244 658/12/658 660/11/660
   --------------------------
 Here is the code I use for reading that :

Here is my function for reading OBJ faces: alias char[] string; void read_face( string data, Group target ) { if ( target is null ) throw new Exception("Face data with no group =

 found!" );
    string[] info =3D split( data );
    if ( info.length !=3D 3 ) throw new Exception("Malformed face data:=

 data );

    face* f =3D target.addface();
    void  add( int which, string vi, string ci )
    {
      f.v[which] =3D atoi(vi)-1;
      f.c[which] =3D atoi(ci)-1;
    }

    void add_info( int index )
    {
      string s =3D info[index];
      string[] arr =3D split( s, "/" );
      if ( arr.length<2 ) throw new Exception("Malformed face data  =

 group:"~ s );
      else add( index, arr[0], arr[1] );
    }

    add_info( 0 );
    add_info( 1 );
    add_info( 2 );
 }

 -DavidM

Thank you for reply. I still haven't solved that sscanf problem event with C strings. I tried to replicate the problem with a new project, and it worked fine.= = Still I used exactly the same code ! Can anyone take a look at the code, here is the link for the projects = http://adogames.com/programming/problem.zip (One of them uses - with = problem uses Derelict )
Jul 06 2006
parent reply DMINATOR <kna_int mail.ru> writes:
On Thu, 06 Jul 2006 21:17:09 +0300, DMINATOR <kna_int mail.ru> wrote:

 On Wed, 05 Jul 2006 14:22:02 +0300, David Medlock <noone nowhere.com> =

 wrote:

 DMINATOR wrote:
 Hi
  I have a little strange problem, probably I am doing something wron=



  I am writing a small parser for an 3d .OBJ format. I am using sscan=



 for
 parsing. Most of the results are correct, except one place.
  Here is the part of input text file:
  g object_1
 f 12/934/12 228/1/228 231/936/231
 f 228/1/228 72/75/72 229/935/229
 f 231/936/231 229/935/229 75/937/75
 f 227/2/227 231/936/231 230/10/230
 f 72/75/72 695/960/695 1346/3/1346
 f 695/960/695 2/1352/2 1345/1328/1345
 f 1346/3/1346 1345/1328/1345 73/938/73
 f 229/935/229 1346/3/1346 233/939/233
 f 654/7/654 232/6/232 75/937/75
 f 233/939/233 236/4/236 654/7/654
 f 75/937/75 233/939/233 654/7/654
 f 644/5/644 236/4/236 233/939/233
 f 73/938/73 234/1340/234 644/5/644
 f 233/939/233 73/938/73 644/5/644
 f 236/4/236 644/5/644 647/940/647
 f 644/5/644 234/1340/234 645/859/645
 f 647/940/647 645/859/645 646/234/646
 f 643/8/643 647/940/647 235/1032/235
 f 232/6/232 654/7/654 656/941/656
 f 654/7/654 236/4/236 643/8/643
 f 656/941/656 643/8/643 655/231/655
 f 653/942/653 656/941/656 655/231/655
 f 666/15/666 242/943/242 71/9/71
 f 230/10/230 244/16/244 666/15/666
 f 71/9/71 230/10/230 666/15/666
 f 658/12/658 244/16/244 230/10/230
 f 75/937/75 232/6/232 658/12/658
 f 230/10/230 75/937/75 658/12/658
 f 244/16/244 658/12/658 660/11/660
   --------------------------
 Here is the code I use for reading that :

Here is my function for reading OBJ faces: alias char[] string; void read_face( string data, Group target ) { if ( target is null ) throw new Exception("Face data with no group=


 found!" );
    string[] info =3D split( data );
    if ( info.length !=3D 3 ) throw new Exception("Malformed face data=


 data );

    face* f =3D target.addface();
    void  add( int which, string vi, string ci )
    {
      f.v[which] =3D atoi(vi)-1;
      f.c[which] =3D atoi(ci)-1;
    }

    void add_info( int index )
    {
      string s =3D info[index];
      string[] arr =3D split( s, "/" );
      if ( arr.length<2 ) throw new Exception("Malformed face data  =


 group:"~ s );
      else add( index, arr[0], arr[1] );
    }

    add_info( 0 );
    add_info( 1 );
    add_info( 2 );
 }

 -DavidM

Thank you for reply. I still haven't solved that sscanf problem event with C strings. I tried to replicate the problem with a new project, and it worked fin=

 Still I used exactly the same code !

 Can anyone take a look at the code, here is the link for the projects =

 http://adogames.com/programming/problem.zip (One of them uses - with  =

 problem uses Derelict )

Sorry that was http://adogames.com/programing/problem.zip P.S. Is there any D sscanf analog ?
Jul 06 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"DMINATOR" <kna_int mail.ru> wrote in message 
news:op.tb95hmzvfa6o3s kna-x0tr7o23p0o.infonet.ee...
On Thu, 06 Jul 2006 21:17:09 +0300, DMINATOR <kna_int mail.ru> wrote:

P.S. Is there any D sscanf analog ?

There is, but for some reason, it's not a separate function.  It's defined 
in std.stream.Stream, and is the method .readf().  If your input is a Stream 
(such as a std.stream.File), you can use readf to extract data from it. 
Jul 06 2006