Validating a character as a newline

How to check if a character is a newline in any encoding in C?

I have the task of writing my own wc program . And if I use only if it (s[i] == '\n')has a different answer than the original wc , if I call it myself. Here is the code:

typedef struct
{
    int newline;
    int word;
    int byte;
} info;

info count(int descr)
{
    info kol;
    kol.newline = 0;
    kol.word = 0;
    kol.byte = 0;

    int len = 512;
    char s[512];
    int n;

    errno = 0;
    int flag1 = 1;
    int flag2 = 1;
    while(n = read(descr, s, len))
    {
        if(n == -1)
            error("Error while reading.", errno);

        errno = 0; 

        kol.byte+=n;
        for(int i=0; i<n; i++)
        {
            if(flag1)
            {
                kol.newline++;
                flag1 = 0;
            }

            if(isblank(s[i]) || s[i] == '\n')
                flag2 = 1;
            else
            {
                if(flag2)
                {
                    kol.word++;
                    flag2 = 0;
                }
            }
            if(s[i] == '\n')
                flag1 = 1;
        }
    }
    return kol;
}  

It works fine for all text files, but when I call it on the file I got after compilation, it does not give wc answer .

+5
source share
3 answers

The way to check if a s[i]character is a newline is simple:

if (s[i] == '\n')

, ( stdin), , , '\n'.

, wc, '\n', , wc. , , . , .

, , - - , Unix Windows. wc .

+5

ASCII Unicode .

\r \n, ASCII. . Windows \r\n ( 0, , ), unix \n. ( ) \r.

. .

. U + 000A U + 000B \r \n ( UTF-8). U + 0085 " ", U + 2028 " " ​​ U + 2029 " ". (U + 000B), . . : http://en.wikipedia.org/wiki/Newline#Unicode

+2

, , isXXXXX() ( isspace(), (, , ...). '\n' , , , '\ r' ( ). UNIX - '\n', Mac ( OS X) '\ r' ( '\n' , '\ r' , MS Office), DOS/Windows "\ r\n".

+1

All Articles