|
CS 456 - Systems Programming
Spring 2024
|
Displaying ./code/jan24/mycat.c
// a simple implentation of the cat command
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
//check if file was provided , otherwise print usage statement and exit
if(argc < 2){
fprintf(stderr, "Usage: %s file\n", argv[0]);
exit(1);
}
//read in a file from the command line
int count = 0;
FILE *fp = fopen(argv[1], "r");
if (fp == NULL){
fprintf(stderr, "File %s could not be opened\n", argv[1]);
exit(1);
}
int c;
//count each character
while((c =fgetc(fp))!= EOF){
printf("%c", c);
}
fclose(fp);
//print the count and exit
//printf("Characters in %s: %d\n", argv[1], count);
return 0;
}
|