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]);









share|improve this question






















  • probably best to strip the newline after fgets and before strtok. Like here
    – AShelly
    Nov 10 at 0:59















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]);









share|improve this question






















  • probably best to strip the newline after fgets and before strtok. Like here
    – AShelly
    Nov 10 at 0:59













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]);









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 0:49









Ventura

13




13












  • 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
















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












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] = '';
}





share|improve this answer























  • 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 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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] = '';
}





share|improve this answer























  • 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 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















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] = '';
}





share|improve this answer























  • 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 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













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] = '';
}





share|improve this answer














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] = '';
}






share|improve this answer














share|improve this answer



share|improve this answer








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 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










  • 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 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 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 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff