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
| #include <stdio.h> #include <string.h> #include <zconf.h>
#define MAX_LINE 1024 #define DEFAULT_SIZE 100
int main(int argc, char *argv[]) { char name[DEFAULT_SIZE] = {0}; char data[DEFAULT_SIZE] = {0};
if (argc == 3) { strcpy(name, argv[1]); strcpy(data, argv[2]); } else { printf("Please input the file name to match: "); scanf("%s", name); printf("Please input the string to match: "); scanf("%s", data); }
char path_in[DEFAULT_SIZE] = {0}; char path_out[DEFAULT_SIZE] = {0}; getcwd(path_in, DEFAULT_SIZE); getcwd(path_out, DEFAULT_SIZE); strcat(path_in, "/"); strcat(path_out, "/"); strcat(path_in, name); strcat(path_out, name); strcat(path_out, ".tmp.txt");
FILE *fin = NULL; FILE *fout = NULL; if ((fin = fopen(path_in, "r")) == NULL) { printf("Open input file error!\n"); return -1; } if ((fout = fopen(path_out, "w")) == NULL) { printf("Open output file error!\n"); return -1; } printf("Doing\n"); while (!feof(fin)) { char line[MAX_LINE]; fgets(line, MAX_LINE, fin); if (strstr(line, data) != NULL) { fprintf(fout, line); } } fclose(fin); fclose(fout); printf("Done. \n");
}
|