-1

im writing code in C that reads a text file then outputs a new text file but im reading my data in as a string and i then need to find a character. is there a way to do this? Within the 'buffer' i need to find the character 'R' and take the Int value after is. how will i go about doing that?? thanks this is my code so far:

const char ch = 'R';
char *ret;


if(strcmp(data_in,"G3")==0)
    {
        if(fgets(buffer,60,p)!= NULL)
        {


            size_t len = strlen(buffer);
            if (len > 0 && buffer[len-1] == '\n')
            {
                buffer[--len] = '\0';
            }

            ret= strchr(buffer, ch);
            fprintf(o,"CC X0.0 Y%s.0\n",ret);
            fprintf(o,"C%s DR+ R0 M\n",buffer);
        }
    }
Benk Yee
  • 11
  • 2

1 Answers1

0

Use sscanf to read the integer after the R.

ret = strchr(buffer, ch);
if (ret) {
    int num;
    sscanf(ret+1, "%d", &num);
    fprintf(o, "CC X0.0 Y%d.0\n", num);
}
fprintf(o, "C%s DR+ R0 M\n", buffer);
Barmar
  • 741,623
  • 53
  • 500
  • 612