C program to count the lines in the file using system calls

advertisements

I am writing a C program to count the lines in files using system calls like open(), read(), write() close(). The same program I did with library calls fopen(), fread(), fwrite() and was working great, but with just system calls, I am stuck.

int fd1;    // file descriptor
fd1=open("f1.txt",O_RDONLY);   // opening file
read(fd1, buffer , 1);   // reading 1 byte from file
// now comparing
if (buffer == '\n')
line++;

My problem is here:

if (myb == '\n')

I do not know how to compare data from buffer. I am trying to use buffer, but no success. Kindly help!


You compared a pointer (buffer) to char ('\n')

You should dereference the pointer, for example:

if (*buffer == '\n')

or

if (buffer[0] == '\n')