sth
This commit is contained in:
39
lab_2/main_static.c
Normal file
39
lab_2/main_static.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "textlib_static.h"
|
||||
|
||||
#define MAX_LINE 4096
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 4) {
|
||||
fprintf(stderr, "Usage: %s <input.txt> <output.txt> <max_replacements>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
FILE *fin = fopen(argv[1], "r");
|
||||
if (!fin) { perror("fopen input"); return 1; }
|
||||
FILE *fout = fopen(argv[2], "w");
|
||||
if (!fout) { perror("fopen output"); fclose(fin); return 1; }
|
||||
int cap = atoi(argv[3]);
|
||||
if (cap < 0) { fprintf(stderr, "invalid cap\n"); fclose(fin); fclose(fout); return 1; }
|
||||
|
||||
int total = 0;
|
||||
char line[MAX_LINE];
|
||||
while (fgets(line, sizeof(line), fin) && cap > 0) {
|
||||
if (line[0] == '\0' || line[0] == '\n') {
|
||||
fputs(line, fout);
|
||||
continue;
|
||||
}
|
||||
int key = (unsigned char)line[0];
|
||||
int repl_line = replace_char_line(line + 1, key, &cap);
|
||||
total += repl_line;
|
||||
fputc(line[0], fout);
|
||||
fputs(line + 1, fout);
|
||||
}
|
||||
while (fgets(line, sizeof(line), fin)) {
|
||||
fputs(line, fout);
|
||||
}
|
||||
fclose(fin); fclose(fout);
|
||||
printf("total_replacements: %d\n", total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user