-1

I have a string that looks like "%i|%i". Some examples: "52|23" , "7|3" , "98|6".

I want to parse this kind of strings to two int variables. so "52|23" will become a variable. int a=52 and int b=23.

These strings are saved in a .txt-file. How can I parse them out and parse them like explained above?

Domien
  • 395
  • 5
  • 18

1 Answers1

0

Just use fscanf:

FILE *f = fopen("file.txt", "r");
if(!f) {
    /* file open failed */
}
int a, b;
while(fscanf(f, "%i|%i", &a, &b) == 2) {
    /* do something with a and b */
}
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Awesome . Didn't know about this method lol. Btw, what happens if the string does not match with the pattern? – Domien Oct 14 '15 at 22:42