2

So I have the following code:

printf("the output is: %s.\n", intCAPass);

Which prints the following:

the output is: intercapass
.

So I tried the two following options to get rid of the newline at the end of the string intCAPass:

intCAPass[strlen(intCAPass)- 1] = '\0';
strtok(intCAPass,"\n");

but both give me the following output:

.he output is: intercapass

So I'm wondering whats going on here.

More info:

OS: Linux

The variable intCAPass receive the string from a function that reads a file containing interca\n. So, I use a buffer in which I copy the whole file and at the end I do buffer[len] = '\0'; and at some point I thought it could cause some problems but I don't think so.

user1527152
  • 946
  • 1
  • 13
  • 37

1 Answers1

5
.he output is: intercapass

You have a Windows-style CRLF (\r\n) line ending in the input string. When the final newline \n is removed, the carriage return \r is left in the string. When printed, it moves output back to the start of the line. Thus the final dot overwrites the first letter of the output line.

These should show similar behaviour:

printf("string one: %s.\n", "blah\r\n");
printf("string two: %s.\n", "blah\r");

Check the character before the \n at the end of the string against a \r, and remove that one too.

ilkkachu
  • 6,221
  • 16
  • 30
  • Yep, fixed. Thank you. I actually thought about it but I thought the files were created under linux. That's also why I mentioned the OS. – user1527152 May 10 '17 at 21:31