Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to extract a line from a string which read from a file.

This is the line from a text "N0010 (TOOL NAME: BA6)"
So i just want to extract last "BA6" in a character string.

How do I do it?

What I have tried:

while(!feof(fp))
{
    fgets(fline,200,fp);

    if(strstr(fline,"TOOL NAME:")!=NULL)
    sscanf(fline,"N0010 TOOL NAME:%s",tname);

    if(strstr(fline,"TOOL DIAMETER:")!=NULL)
    sscanf(fline,"N0020 TOOL DIAMETER:%s",tdia);

    if(strstr(fline,"TOOL RADIUS:")!=NULL)
    sscanf(fline,"N0030 TOOL RADIUS:%s",trad);

    if(strstr(fline,"STOCK_PART:")!=NULL)
    sscanf(fline,"N0040 STOCK_PART:%s",spart);
}
  fclose(fp);
Posted
Updated 9-Jan-20 1:32am

1 solution

How on earth
Quote:
scanf(fline,"N0010 TOOL NAME:%s",tname);
could possibly match with
Quote:
N0010 (TOOL NAME: BA6)

(Hint: round braces).
 
Share this answer
 
Comments
Shao Voon Wong 10-Jan-20 6:10am    
5
CPallini 10-Jan-20 6:33am    
Thank you.
Member 14630688 13-Jan-20 0:16am    
I tried doing this. sscanf(fline,"N0010 (TOOL NAME: %s)",tname);

The actual line in text is 'N0010 (TOOL NAME: 10R1)'

In tname I get "10R1)" I dont want the last brace ')' I just want output to be '10R1'
CPallini 13-Jan-20 3:04am    
Yes, sscanf with %s stops only at first whitespace character.
You might try instead:
sscanf(fline,"N0010 (TOOL NAME: %[^)]",tname)

See:
http://www.cplusplus.com/reference/cstdio/scanf/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900