0

Storing value from txt file (C)

I'm working on a code that has to take a value from a txt file, i have to read it with fgets and the txt file is set like this: ITEM: 22 i tried doing something like char line[50]; fgets(line,50,/*nameoffile*/); if(strstr(line,"ITEM: ")){ /*code*/} but then i dont know what to do in the if statement. I'm working on windows,not on code playground.

8th Jul 2020, 8:52 AM
Guido Parlatore
Guido Parlatore - avatar
3 odpowiedzi
+ 1
do you want access 22? if yes then you should try this... char *ptr=strstr(line,"ITEM: ") ; if(ptr) { print(ptr+6); } /* strstr = Returns pointer to first occurrence of "ITEM: " in line. i have stored in a pointer. In the print statement i have written ptr+6. i set the pointer position to the 6 place from its original position. and i have choose 6 because of length of "ITEM: " is 6. */
8th Jul 2020, 4:29 PM
Saurabh
Saurabh - avatar
+ 1
👍😃👍
8th Jul 2020, 4:51 PM
Saurabh
Saurabh - avatar
0
You saved my day
8th Jul 2020, 4:41 PM
Guido Parlatore
Guido Parlatore - avatar