I understand ASCII 13 is \r
and ASCII 10 is \n
for Windows.
I was expecting to see A
only in txt file, but I see two new lines before 'A' gets printed in the line 3. when I run the following code:
#include <stdio.h>
int main(void)
{
FILE* fp;
int ary[3] = {13, 13, 65};
int i, res;
fp = fopen("a.txt", "wb");
for (i = 0; i < 3 ; i++)
{
fputc(ary[i], fp);
}
fclose(fp);
return 0;
}
Where are the two new lines coming from?
I tried with different sets of array like {65, 13, 13, 65} or {65, 13, 10, 65}.
I was expecting to see one A in line 1 when ary is {65, 13, 13, 65}, but I get A in line 1, empty space in line 2 and another A in line3..
The program creates/overwrites a file. Because of the b
, the file will contain exactly the values written to it: 13,13,65.
The above describes what the program does. But your question doesn't appear to be about what the program does; it appears to be about what Notepad does.
Windows line endings consist of the two-byte sequence 13,10. The CR characters are found outside of a line ending, so we're not dealing with a plain text file, but one that contains control characters. This means that how a text file editor will display this file can vary.
A carriage return is a move of the cursor to the home column, which can be used to overwrite characters on some terminals or overprint characters on some printers. But there's no reason for Notepad to act like a terminal or printer. It does not have to emulate a carriage return when it encounters a 13. According to what you say, it interprets the partial line ending as a line ending, which is perfectly acceptable.