forked from yunghsianglu/CProgramExercise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
93 lines (85 loc) · 2.07 KB
/
Copy pathmain.c
File metadata and controls
93 lines (85 loc) · 2.07 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "mystring.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_SIZE 1000 // assume each line has at most 999 characters
int main(int argc, char *argv[])
{
if (argc != 4)
{
printf("usage: %s <command> <inputfile> <outputfile>\n", argv[0]);
return EXIT_FAILURE;
}
FILE *infptr = fopen(argv[2], "r");
if (infptr == NULL)
{
printf("unable to open file %s!\n", argv[2]);
return EXIT_FAILURE;
}
FILE *outfptr = fopen(argv[3], "w");
if (outfptr == NULL)
{
printf("unable to open file %s!\n", argv[3]);
fclose(infptr);
return EXIT_FAILURE;
}
int num_lines = 0;
char buffer[LINE_SIZE];
// count the number of lines in the file
while (fgets(buffer, LINE_SIZE, infptr) != NULL)
{
num_lines++;
}
fseek(infptr, 0, SEEK_SET); // return to the beginning of the file
char **lines = malloc(sizeof(char *) * num_lines);
int i;
for (i = 0; i < num_lines; i++)
{
if (feof(infptr))
{
printf("not enough num_lines in file!\n");
fclose(infptr);
fclose(outfptr);
return EXIT_FAILURE;
}
lines[i] = malloc(sizeof(char) * LINE_SIZE);
fgets(lines[i], LINE_SIZE, infptr);
}
fclose(infptr);
int total_length = 0;
for (i = 0; i < num_lines; i++)
{
total_length += my_strlen(lines[i]);
}
// count the length of each line
if (strcmp(argv[1], "strlen") == 0)
{
for (i = 0; i < num_lines; i++)
{
fprintf(outfptr, "length: %d\n", my_strlen(lines[i]));
}
}
// for each line, count the occurrence of the first letter in the line
if (strcmp(argv[1], "countchar") == 0)
{
for (i = 0; i < num_lines; i++)
{
fprintf(outfptr, "count(%c): %d\n", lines[i][0],
my_countchar(lines[i], lines[i][0]));
}
}
if (strcmp(argv[1], "strupper") == 0)
{
for (i = 0; i < num_lines; i++) {
my_strupper(lines[i]);
fprintf(outfptr, "%s", lines[i]);
}
}
for (i = 0; i < num_lines; i++)
{
free(lines[i]);
}
free(lines);
fclose(outfptr);
return EXIT_SUCCESS;
}