Reputation: 9
So I am currently using strsep() to separate my string where the given string is
char str[] = "line 01\n"
"line 02\n"
"line 03\n"
"line 04\n"
"line 05\n"
"line 06\n"
"line 07\n"
"line 08\n"
"line 09\n"
"line 10\n";
and I am using strsep(&str, "\n")
to separate them. So my concern is that I am actually using two delimiters "\" and "n" right? Then how do I just make the delimiter to be just "\n". Because after extracting "line 10", strsep replaces the "\" with "\0". But str[] itself is terminated by "\0", and one of my delimiters is "\" so it will actually treat that the "\" in "\0" as another string hence I would have extracted an unnecessary empty string.
Upvotes: 0
Views: 1181
Reputation: 881623
Inside string and character literals, escape sequences are processed so that (for example) \n
and \0
become the single characters "newline" and "string terminator" - they are not two characters each as you seem to think.
This important detail is in C11 5.1.1.2 Translation Phases
in phase 5:
Each source character set member and escape sequence in character constants and string literals is converted to the corresponding member of the execution character set; if there is no corresponding member, it is converted to an implementation-defined member other than the null (wide) character.
Also in 5.2.1 Character sets /2
:
In a character constant or string literal, members of the execution character set shall be represented by corresponding members of the source character set or by escape sequences consisting of the backslash \ followed by one or more characters.
The actual escape sequences you are permitted to use are detailed in several places in the standard, I won't show them all here since the answer's probably already big enough.
However, based on a careful reading of your question, I suspect your actual problem is misunderstanding why separating your strings using a newline token results in an empty string after "line 10".
That can be easily understood if you consider the following string:
A|B|C
If you separate that based on |
, you'll end up with the three values A
, B
and C
. If you do the same thing to the string A|B|
, you'll end up with A
, B
and the empty string.
That's almost certainly what's happening with your string. Because your last few characters are line 10\n
and you're using \n
as the separator, there's actually an extra empty string following that final \n
.
I suspect if you take off the final newline, your problem will go away.
Upvotes: 2