Using strtok, last token comes with a line delimiter
up vote
0
down vote
favorite
So when writing this code and showing the last token it comes with the line delimiter "n", how do i take that out?
while( fgets( c, MAX_viagens, f) != NULL ) {
int i = 0;
char *p = strtok (c, ":");
char *array[6];
while (p != NULL){
array[i++] = p;
p = strtok (NULL, ":");
}
printf ("%sn", array[3]);
c strtok
add a comment |
up vote
0
down vote
favorite
So when writing this code and showing the last token it comes with the line delimiter "n", how do i take that out?
while( fgets( c, MAX_viagens, f) != NULL ) {
int i = 0;
char *p = strtok (c, ":");
char *array[6];
while (p != NULL){
array[i++] = p;
p = strtok (NULL, ":");
}
printf ("%sn", array[3]);
c strtok
probably best to strip the newline afterfgets
and beforestrtok
. Like here
– AShelly
Nov 10 at 0:59
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So when writing this code and showing the last token it comes with the line delimiter "n", how do i take that out?
while( fgets( c, MAX_viagens, f) != NULL ) {
int i = 0;
char *p = strtok (c, ":");
char *array[6];
while (p != NULL){
array[i++] = p;
p = strtok (NULL, ":");
}
printf ("%sn", array[3]);
c strtok
So when writing this code and showing the last token it comes with the line delimiter "n", how do i take that out?
while( fgets( c, MAX_viagens, f) != NULL ) {
int i = 0;
char *p = strtok (c, ":");
char *array[6];
while (p != NULL){
array[i++] = p;
p = strtok (NULL, ":");
}
printf ("%sn", array[3]);
c strtok
c strtok
asked Nov 10 at 0:49
Ventura
13
13
probably best to strip the newline afterfgets
and beforestrtok
. Like here
– AShelly
Nov 10 at 0:59
add a comment |
probably best to strip the newline afterfgets
and beforestrtok
. Like here
– AShelly
Nov 10 at 0:59
probably best to strip the newline after
fgets
and before strtok
. Like here– AShelly
Nov 10 at 0:59
probably best to strip the newline after
fgets
and before strtok
. Like here– AShelly
Nov 10 at 0:59
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
One simple way to achieve this is to add the new line character to the delimiters:
char *p = strtok (c, ":n");
...
p = strtok (NULL, ":n");
Or you could remove it before (removes last character, even if it is not 'n'
):
if(c[0])
{
c[strlen(c)-1] = '';
}
I think your first example is the way to go. But,strlen
has an extra cost, so, for your second example, I'd do:int len = strlen(c); if (len) c[len - 1] = 0;
– Craig Estey
Nov 10 at 1:10
@CraigEstey Yes it would be more efficient to callstrlen
only one time. But if you enable optimization I think the compiler should recognize it.
– Osiris
Nov 10 at 1:12
@CraigEstey I changed it to a simpler check.
– Osiris
Nov 10 at 1:20
I think that's a good idea. Because this:#include <string.h> char *c; void funny(char *ptr); int main(void) { for (int idx = 0; idx < strlen(c); ++idx) funny(&c[idx]); return 0; }
doesn't optimize thestrlen
to a single call. Because it can't--it has to assumefunny
can change the string andstrlen
will return a different value. Helping the compiler [rather than _relying on it] is good practice, IMO.
– Craig Estey
Nov 10 at 1:26
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
One simple way to achieve this is to add the new line character to the delimiters:
char *p = strtok (c, ":n");
...
p = strtok (NULL, ":n");
Or you could remove it before (removes last character, even if it is not 'n'
):
if(c[0])
{
c[strlen(c)-1] = '';
}
I think your first example is the way to go. But,strlen
has an extra cost, so, for your second example, I'd do:int len = strlen(c); if (len) c[len - 1] = 0;
– Craig Estey
Nov 10 at 1:10
@CraigEstey Yes it would be more efficient to callstrlen
only one time. But if you enable optimization I think the compiler should recognize it.
– Osiris
Nov 10 at 1:12
@CraigEstey I changed it to a simpler check.
– Osiris
Nov 10 at 1:20
I think that's a good idea. Because this:#include <string.h> char *c; void funny(char *ptr); int main(void) { for (int idx = 0; idx < strlen(c); ++idx) funny(&c[idx]); return 0; }
doesn't optimize thestrlen
to a single call. Because it can't--it has to assumefunny
can change the string andstrlen
will return a different value. Helping the compiler [rather than _relying on it] is good practice, IMO.
– Craig Estey
Nov 10 at 1:26
add a comment |
up vote
2
down vote
accepted
One simple way to achieve this is to add the new line character to the delimiters:
char *p = strtok (c, ":n");
...
p = strtok (NULL, ":n");
Or you could remove it before (removes last character, even if it is not 'n'
):
if(c[0])
{
c[strlen(c)-1] = '';
}
I think your first example is the way to go. But,strlen
has an extra cost, so, for your second example, I'd do:int len = strlen(c); if (len) c[len - 1] = 0;
– Craig Estey
Nov 10 at 1:10
@CraigEstey Yes it would be more efficient to callstrlen
only one time. But if you enable optimization I think the compiler should recognize it.
– Osiris
Nov 10 at 1:12
@CraigEstey I changed it to a simpler check.
– Osiris
Nov 10 at 1:20
I think that's a good idea. Because this:#include <string.h> char *c; void funny(char *ptr); int main(void) { for (int idx = 0; idx < strlen(c); ++idx) funny(&c[idx]); return 0; }
doesn't optimize thestrlen
to a single call. Because it can't--it has to assumefunny
can change the string andstrlen
will return a different value. Helping the compiler [rather than _relying on it] is good practice, IMO.
– Craig Estey
Nov 10 at 1:26
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
One simple way to achieve this is to add the new line character to the delimiters:
char *p = strtok (c, ":n");
...
p = strtok (NULL, ":n");
Or you could remove it before (removes last character, even if it is not 'n'
):
if(c[0])
{
c[strlen(c)-1] = '';
}
One simple way to achieve this is to add the new line character to the delimiters:
char *p = strtok (c, ":n");
...
p = strtok (NULL, ":n");
Or you could remove it before (removes last character, even if it is not 'n'
):
if(c[0])
{
c[strlen(c)-1] = '';
}
edited Nov 10 at 1:18
answered Nov 10 at 0:59
Osiris
1,660414
1,660414
I think your first example is the way to go. But,strlen
has an extra cost, so, for your second example, I'd do:int len = strlen(c); if (len) c[len - 1] = 0;
– Craig Estey
Nov 10 at 1:10
@CraigEstey Yes it would be more efficient to callstrlen
only one time. But if you enable optimization I think the compiler should recognize it.
– Osiris
Nov 10 at 1:12
@CraigEstey I changed it to a simpler check.
– Osiris
Nov 10 at 1:20
I think that's a good idea. Because this:#include <string.h> char *c; void funny(char *ptr); int main(void) { for (int idx = 0; idx < strlen(c); ++idx) funny(&c[idx]); return 0; }
doesn't optimize thestrlen
to a single call. Because it can't--it has to assumefunny
can change the string andstrlen
will return a different value. Helping the compiler [rather than _relying on it] is good practice, IMO.
– Craig Estey
Nov 10 at 1:26
add a comment |
I think your first example is the way to go. But,strlen
has an extra cost, so, for your second example, I'd do:int len = strlen(c); if (len) c[len - 1] = 0;
– Craig Estey
Nov 10 at 1:10
@CraigEstey Yes it would be more efficient to callstrlen
only one time. But if you enable optimization I think the compiler should recognize it.
– Osiris
Nov 10 at 1:12
@CraigEstey I changed it to a simpler check.
– Osiris
Nov 10 at 1:20
I think that's a good idea. Because this:#include <string.h> char *c; void funny(char *ptr); int main(void) { for (int idx = 0; idx < strlen(c); ++idx) funny(&c[idx]); return 0; }
doesn't optimize thestrlen
to a single call. Because it can't--it has to assumefunny
can change the string andstrlen
will return a different value. Helping the compiler [rather than _relying on it] is good practice, IMO.
– Craig Estey
Nov 10 at 1:26
I think your first example is the way to go. But,
strlen
has an extra cost, so, for your second example, I'd do: int len = strlen(c); if (len) c[len - 1] = 0;
– Craig Estey
Nov 10 at 1:10
I think your first example is the way to go. But,
strlen
has an extra cost, so, for your second example, I'd do: int len = strlen(c); if (len) c[len - 1] = 0;
– Craig Estey
Nov 10 at 1:10
@CraigEstey Yes it would be more efficient to call
strlen
only one time. But if you enable optimization I think the compiler should recognize it.– Osiris
Nov 10 at 1:12
@CraigEstey Yes it would be more efficient to call
strlen
only one time. But if you enable optimization I think the compiler should recognize it.– Osiris
Nov 10 at 1:12
@CraigEstey I changed it to a simpler check.
– Osiris
Nov 10 at 1:20
@CraigEstey I changed it to a simpler check.
– Osiris
Nov 10 at 1:20
I think that's a good idea. Because this:
#include <string.h> char *c; void funny(char *ptr); int main(void) { for (int idx = 0; idx < strlen(c); ++idx) funny(&c[idx]); return 0; }
doesn't optimize the strlen
to a single call. Because it can't--it has to assume funny
can change the string and strlen
will return a different value. Helping the compiler [rather than _relying on it] is good practice, IMO.– Craig Estey
Nov 10 at 1:26
I think that's a good idea. Because this:
#include <string.h> char *c; void funny(char *ptr); int main(void) { for (int idx = 0; idx < strlen(c); ++idx) funny(&c[idx]); return 0; }
doesn't optimize the strlen
to a single call. Because it can't--it has to assume funny
can change the string and strlen
will return a different value. Helping the compiler [rather than _relying on it] is good practice, IMO.– Craig Estey
Nov 10 at 1:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235040%2fusing-strtok-last-token-comes-with-a-line-delimiter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
probably best to strip the newline after
fgets
and beforestrtok
. Like here– AShelly
Nov 10 at 0:59