-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?

0

1 Answer 1

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 */
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome . Didn't know about this method lol. Btw, what happens if the string does not match with the pattern?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.