For simplicity I suggest you do it in multiple passes over each line, where each pass copies part of the line into temporary array.
For example an initial pass to remove a trailing semi-colon if one exists (for this you don't actually need to copy). Then a pass to copy all but the opening and closing double-quotes "
. Then one pass for the braces. And one pass for the back-slashes (and the n
in
). And a last pass for the single quotes.
All that should leave you with something like "Chuck Norris, norrischuck, 100"
. And this can be fed to strtok
to "tokenize" on the comma, and you simply call it twice to get the three separate strings "Chuck Norris"
, "norrischuck"
, and "100"
. The last you could pass to strtoul
to convert to an integer.
You can of course combine all the passes into a single pass once you get the long multi-pass solution working.
When I say you make a "pass" over the input, I mean you iterate over the string, copying all but the unwanted characters to a new temporary array.
For example:
// Previous pass puts its output in pass_1_output
// Pass to remove double-quotes
char pass_2_ouput[1000] = { 0 }; // Zero-initialize, which is the string terminator
for (size_t in = 0, out = 0; pass_1_output[in] != ''; ++in)
{
if (pass_1_input[in] != '"')
{
// Not a double-quote, copy the input to the output
pass_2_output[out++] = pass_1_input[i];
}
}
// After the above loop, pass_2_output will contain the same contents as
// pass_1_output, *except* any double-quotes
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…