File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments