Hi,
I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves through program code. argc and argv look at one line at a time and they only look at what is on the command line. I am not sure how a program is broken up into files for WC to read.
this is the code I have so far:
Code:
/* wc simulate */
#include <stdio.h>
#include <stdlib.h>
char *pgmname; /* name of this program */
int line_count = 0;
int word_count = 0;
int byte_count = 0;
FILE *fp;
void main(argc, argv)
int argc; char *argv[];
{
int i;
char *cp;
pgmname = argv[0];
fp = stdin;
for(i = 1; i < argc; i++) {
cp = argv[i];
if(*cp == '-'){
if(*++cp == '\n'){
line_count++;}
}
else {
if(fp != stdin) {
fprintf(stderr, "%s: too many arguments\n", pgmname);
exit(1);
}
fp = fopen(cp, "r")
if(fp == NULL) {
fprintf(stderr, "%s: unable to read %s\n", pgmname, cp);
exit(1);
}
}
printf("%d\n", line_count);
}
}
Right now I have code to just count the lines. I am testing the program on a hello world program below:
Code:
#include <stdio.h>
main()
{
printf("Hello World!\n");
}
when I test the program, I get 0 for line count.