Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <errno.h>
- #include <assert.h>
- #include <stdlib.h>
- void printfile(char *file);
- void invalidArgs();
- int main(int argc, char *argv[])
- {
- if (argc==1)
- {
- invalidArgs();
- }
- int i;
- int keys=0;
- for (int i = 1; i < argc; ++i)
- {
- if (argv[i][0]!='-') { printfile(argv[i]); }
- else keys++;
- }
- if (keys==argc-1)
- {
- invalidArgs();
- }
- exit(0);
- return 0;
- }
- void printfile(char *file){
- assert(file!=NULL);
- FILE *fp = fopen(file, "rb");
- if (!fp)
- {
- fprintf(stderr, "Can't open input file %s!\n", file);
- exit(ENOENT);
- }
- int buff;
- while ((buff=fgetc(fp)) != EOF){
- printf("%c", buff);
- }
- fclose(fp);
- }
- void invalidArgs(){
- fprintf(stderr, "usage: printfiles file1 [file2 ...]\n file1, file2, etc.: files to print\n");
- exit(EINVAL);
- }
Advertisement
Add Comment
Please, Sign In to add comment