Skip to content

Commit 77938e7

Browse files
committed
new program added
1 parent 91cb93a commit 77938e7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

questions/question23.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
A file "input.txt" contains a some lines of text. Collect the name of the file from the user, and then
3+
use the C "system()" function, and the standard UNIX command "wc", to find the number
4+
of lines, words and characters in the file. Ensure that the file is readable before trying the "system()"
5+
function.
6+
INPUT: read file name from command line as: ./a.out input.txt
7+
OUTPUT: the number of lines, words characters in the file "input.txt" [2 marks]
8+
9+
*/
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
14+
int main(int argc, char *argv[]){
15+
16+
char *str = malloc(sizeof(char)*100);
17+
18+
strcpy(str, "wc ");
19+
strcat(str, argv[1]);
20+
21+
printf("%s\n", str);
22+
23+
if(argc > 1){
24+
FILE *fp = fopen(argv[1],"r");
25+
if(fp != NULL){
26+
system(str);
27+
}
28+
fclose(fp);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)