No, '\n' is not "twice as large".
The problem is that you are reading a file with Windows line endings ('\r' '\n') in text mode, so your fread will never see the '\r' characters.
However, the fseek and ftell have no way to ignore the '\r' chars, so they count them and therefore you need to subtract 2 for every '\n' you see.
It would be more portable to open the file in binary mode and explicitly deal with the '\r' chars.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
long int countSigns(FILE *fin)
{
fseek(fin, 0, SEEK_END);
long int pos = ftell(fin);
fseek(fin, 0, SEEK_SET);
for (char ch; fread(&ch, 1, 1, fin) == 1; )
if (ch == ' ' || ch == '\n' || ch == '\r') // or isspace(ch)
--pos;
return pos;
}
// A more usual way to do it. (Also works fine in text mode.)
long countNonWhitespace(FILE *fin) {
long count = 0;
for (int c; (c = fgetc(fin)) != EOF; )
if (!isspace(c))
++count;
return count;
}
int main()
{
FILE *fin = fopen("count.c", "rb"); // opening in binary mode
if (!fin) { perror("fopen"); exit(EXIT_FAILURE); }
printf("count: %ld\n", countNonWhitespace(fin));
rewind(fin);
printf("Zeichenzahl: %ld\n", countSigns(fin));
fclose(fin);
return 0;
}