-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconcat.c
More file actions
38 lines (32 loc) · 778 Bytes
/
concat.c
File metadata and controls
38 lines (32 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// implementation of cat method
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
void print_error(char *this, char *filename){
fprintf(stderr, "%s cannot concat %s\n%s\n",
this, filename, strerror(errno));
exit(EXIT_FAILURE);
}
void print_usage(char *this){
fprintf(stderr, "Syntax Error:\nUsage: %s [filename]", this);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]){
errno = 0;
FILE *fp;
int maxline = 1024;
char line[maxline];
if(argc == 2){
if((fp = fopen(argv[1], "rb")) == NULL){
print_error(argv[0], argv[1]);
}
}
else{
print_usage(argv[0]);
}
while(fgets(line, maxline, fp)){
printf("%s", line);
}
fclose(fp);
}