I want to make a very simple console application that replaces the Unix command wc -c
. For this, I need simply to count the bytes into the file and output them in the console.
If my application is not provided with arguments, it reads them from the standard input (this part I've already made working). However, if I decide to supply more arguments (valid and existing file names), I should count each one of them their bytes.
So far so good. Here is the code
const int __OPEN_ERROR = 48;
int main(int argc, char* argv[]) {
if(argc == 1) {
char buf[3];
int count = 0, lines = 0;
while(read(0, buf, 1) != 0) {
count++;
if(buf[0] == '\n')
lines++;
}
printf("Count:%d\n", count);
printf("Lines:%d\n", lines);
}
else {
int fd, bytes = 0;
char buf[3];
for (int arg = 1; arg <= argc; arg++) {
if ((fd = open(argv[arg], O_RDONLY) < 0)) {
exit(__OPEN_ERROR);
}
while (read(fd, buf, 1) != 0) { // <-- this here leads to infinite loop.
printf("%s", buf);
bytes++;
}
printf("%d %s", bytes, argv[arg]);
}
}
exit(0);
}
Please don't mind how badly this code is written, this is my first day exploring C, for now everything I want is working solution.
Notice how both reads are same but latter one falls into infinite loop, for a reason I cannot understand. And yes, opening the file succeeds.