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 .
source
share