I have searched thoroughly for an answer to my problem but without much success. I was hoping someone here can help me out.
I have a simple code that reads 1 line from a file & calculates the number of characters & words in that line. I use the whitespace character to determine when a new word starts & the \n to determine when the line ends.
For some reason, the whitespace is never detected & the program goes in an infinite loop.
This happens if I initialize the buffer to \n. If I dont do that, even the \n is not detected.
Thanks in advance.
memset(&buf[0], '\n', sizeof(buf));
read(fd, &buf[0], sizeof(buf));
while(buf[i] != '\n') {
while(buf[i] != ' ') {
no_of_chars++;
i++;
}
no_of_words++;
i++;
}
Contents of the file I am reading: “This is a test file”
Compiler: GCC (Ubuntu)
There are two problems: