简体   繁体   中英

Need some help parsing a string in C

Using fgets, I have read in a line from a text file. The line may be something like this:

# O^6+ + H -> O^5+ + H^+

Or it may be this:

# Mg^12+ + H -> Mg^11+ + H^+

or this:

# Ne^10+ + He -> Ne^9+ + He^+

Or a multitude of other possibilities.

I am trying to extract the ion, the charge and the atom terms from the string.

I tried something like this:

sscanf(line,"# %2s^%d+ + %2s",cs->ION,&(cs->Z),cs->ATOM);

I also tried this:

sscanf(line,"# %[^^]s^%d+ + %2s",cs->ION,&(cs->Z),cs->ATOM); Because I was picking up the '^' character.

I just can't seem to get this to work for every case. Any suggestions are appreciated.

Your try with the format string

"# %[^^]s^%d+ + %2s"

was almost right, except that after the %[^^] there has to be no s , ie

"# %[^^]^%d+ + %2s"

works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM