-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion23.c
31 lines (24 loc) · 851 Bytes
/
question23.c
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
/*
A file "input.txt" contains a some lines of text. Collect the name of the file from the user, and then
use the C "system()" function, and the standard UNIX command "wc", to find the number
of lines, words and characters in the file. Ensure that the file is readable before trying the "system()"
function.
INPUT: read file name from command line as: ./a.out input.txt
OUTPUT: the number of lines, words characters in the file "input.txt" [2 marks]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
char *str = malloc(sizeof(char)*100);
strcpy(str, "wc ");
strcat(str, argv[1]);
printf("%s\n", str);
if(argc > 1){
FILE *fp = fopen(argv[1],"r");
if(fp != NULL){
system(str);
}
fclose(fp);
}
}