P
P
pshevnin2021-11-11 13:46:45
C++ / C#
pshevnin, 2021-11-11 13:46:45

How to parse a string in C?

Good afternoon. There is a format string:
828,2065,2001,1000, True The, "LINESTRING (13.3667747154831 49.6852257801913 49.6858398295424,13.3636960233053 49.6855263919521,13.3627006339596)"
We need to break it into parts between the commas, but ignore everything after True. How can this be done in C language? I tried strtok(), but it fails to ignore the unwanted part of the string.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2021-11-11
@pshevnin

sscanf(str, "%d,%d,%d,%d,True", ...);

R
Ronald McDonald, 2021-11-11
@Zoominger

Yes, everything is simple. Cut off everything that comes after the letter "T" (you search through strchr, and trimming is easy to google), then for the rest sequentially (while or for loop) go through each character. If it is not equal to a comma, then add a character to the temporary string (which you can create right there in the loop). Each such line will be the desired numbers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question