Any chance you are running this on an OS with a “new line” character that isn’t “\n”? Windows for example could be “\r\n”. I tried on Linux and it worked, although I only grabbed the necessary parts, so if something else is breaking, I can’t say. Or perhaps I misunderstood the issue altogether. Here is what I see:
The function:
# code blockintprint_file(char* buffer){
printf("print_file\n");
int line_num = 1;
char* line = strtok(buffer, "\n");
while(line != NULL) {
printf("%4d\t%s\n", line_num, line);
line = strtok(NULL, "\n");
line_num++;
}
return0;
}
Any chance you are running this on an OS with a “new line” character that isn’t “\n”? Windows for example could be “\r\n”. I tried on Linux and it worked, although I only grabbed the necessary parts, so if something else is breaking, I can’t say. Or perhaps I misunderstood the issue altogether. Here is what I see:
The function:
# code block int print_file(char* buffer) { printf("print_file\n"); int line_num = 1; char* line = strtok(buffer, "\n"); while(line != NULL) { printf("%4d\t%s\n", line_num, line); line = strtok(NULL, "\n"); line_num++; } return 0; }And the output:
# code block test file: test.txt hello 123 123 123 asdf asdf asdf 0oijf0w0j s0w0wfjwef 0wefjfjwefjkwejijiowefjo output: 1 hello 123 123 123 2 asdf asdf asdf 3 0oijf0w0j s0w0wfjwef 4 0wefjfjwefjkwejijiowefjo count: 4 lines | 0 words | 82 characters