-1

hi lets say ı have a input like this:

set 15,10,55,20,30,-30,1
move 0,0
move 2,-1
move 0,0
move 1,-3
move 0,0
move 0,0
move 1,2
move 0,0
move 0,0
move 1,0
changemode 2
move 0,0
move 0,0
move 0,3
move 0,0
goback 1
changespeed 40,40
...
while(1)
    {
        if(fgets(command,128,fptr) != 0)
            {
                if(strncmp(command,"move",4)==0)
                    {
                        fscanf(fptr,"%s %d %d",inputcommand,firstargument,secondargumant);                        
                    }
                if(strncmp(command,"set",3)==0)
                    {
                     fscanf(fptr,"%s %d ....%d",inputcommand,firstargument,secondargumant........,sevenargument);   
                    }
              else{break;}
            }

fscanf returns to the first line and cant read the arguments how to get the arguments on the line that i read

  • 1
    possible duplicate of [read file in C and split string](http://stackoverflow.com/questions/15957469/read-file-in-c-and-split-string) – Dylan Corriveau Mar 12 '15 at 12:51
  • 1
    That wouldn't work as good as you think, you should read about **flex** and **bison** or the approproate lexer/parser generator for your platform, it will help you save time, and you will create a robust parser. – Iharob Al Asimi Mar 12 '15 at 12:52
  • 1
    Why are you using `fscanf` instead of `sscanf`? – William Pursell Mar 12 '15 at 13:25

2 Answers2

0

Don't use scanf. Use strtol. If you pass a non-null value to the second argument, strtol will populate it with the address of the comma, and you can continue parsing from there.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

dont use fscanf use sscanf instead

if(strncmp(command,"move",4)==0)
                    {
                       sscanf(command,"%s %d %c %d",input,&a,&comma,&b);
                    }

with sscanf() u can divide the string as you want

Alper Fırat Kaya
  • 2,079
  • 1
  • 18
  • 18