C using scanf() for | delimited string

后端 未结 3 843
小蘑菇
小蘑菇 2021-01-26 12:04

I want to input a few strings then two integers. Whilst the strings are separated by \'|\', the integers are kept apart by a \'.\'.

Looking around online I have seen s

3条回答
  •  北海茫月
    2021-01-26 12:29

    This seems to work...

    #include 
    
    main()
    {
    
    char x[32] = "abc|def|123.456.";
    char y[20];
    char z[20];
    int i =0;
    int j =0;
    sscanf(x,"%[^|]|%[^|]|%d.%d.",y,z,&i,&j);
    fprintf(stdout,"1:%s 2:%s 3:%d 4:%d\n",y,z,i,j);
    
    }
    

提交回复
热议问题