How to use strstr() function to compare content in character arrays in C? -
the purpose of small program compare strings in 2 separate files (file1.csv , file2.csv), , gives results file3.csv. if strings in file2.csv found in file1.csv, program copies strings in file1.csv file3.csv.
content of file1.csv (with 6 columns):
10000001 text1 text1 text1 text1 text1 10000002 text2 text2 text2 text2 text2 10000003 text3 text3 text3 text3 text3 10000004 text4 text4 text4 text4 text4 10000005 text5 text5 text5 text5 text5 10000006 text6 text6 text6 text6 text6 10000007 text7 text7 text7 text7 text7 10000008 text8 text8 text8 text8 text8 10000009 text9 text9 text9 text9 text9 10000010 text10 text10 text10 text10 text10 10000011 text11 text11 text11 text11 text11 10000012 text12 text12 text12 text12 text12 10000013 text13 text13 text13 text13 text13 10000014 text14 text14 text14 text14 text14 10000015 text15 text15 text15 text15 text15 10000016 text16 text16 text16 text16 text16 10000017 text17 text17 text17 text17 text17 10000018 text18 text18 text18 text18 text18 10000019 text19 text19 text19 text19 text19 10000020 text20 text20 text20 text20 text20 10000021 text21 text21 text21 text21 text21 10000022 text22 text22 text22 text22 text22 10000023 text23 text23 text23 text23 text23 10000024 text24 text24 text24 text24 text24 10000025 text25 text25 text25 text25 text25
content of file2.csv (only 1 column):
10000001 10000003 10000004 10000006 10000007 10000008 10000009 10000011 10000012 10000015 10000025
the expected results be:
10000001 text1 text1 text1 text1 text1 10000003 text3 text3 text3 text3 text3 10000004 text4 text4 text4 text4 text4 10000006 text6 text6 text6 text6 text6 10000007 text7 text7 text7 text7 text7 10000008 text8 text8 text8 text8 text8 10000009 text9 text9 text9 text9 text9 10000011 text11 text11 text11 text11 text11 10000012 text12 text12 text12 text12 text12 10000015 text15 text15 text15 text15 text15 10000025 text25 text25 text25 text25 text25
i used strstr() function compare strings didn't work, code listed below:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char line[150][80] = {0}; char line2[150][80] = {0}; int = 0, b = 0; file *file1 = fopen("file1.csv", "r"); file *file2 = fopen("file2.csv", "r"); file *file3 = fopen("file3.csv", "w"); while (fscanf(file1, "%79[^\n]\n", line[i]) != eof) { i++; while (fscanf(file2, "%79[^\n]\n", line2[b]) != eof) { b++; } if (strstr(line[i],line2[b])) fprintf(file3, "%s\n", line[i]); } fclose(file1); fclose(file2); fclose(file3); return 0; }
thanks in advance!
as unwind , other commenters rightly noted, there's no point re-reading file2.csv
on every file1.csv
reading iteration. read once , loop through line2[]
array then.
second, may process file1.csv
line-by-line , forget current line's contents it's been processed, making line[][]
array unnecessary.
here's amended code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define check_file_open(file, name) \ if (file == null) { \ printf("failed open %s\n", name); \ return 1; \ } int main() { char line[150] = ""; char line2[150][80] = {0}; int b = 0; int filtercount = 0; file *file1 = fopen("file1.csv", "r"); check_file_open(file1, "file1.csv"); file *file2 = fopen("file2.csv", "r"); check_file_open(file2, "file2.csv"); file *file3 = fopen("file3.csv", "w"); check_file_open(file3, "file3.csv"); while (fscanf(file2, "%79[^\n]\n", line2[b]) != eof) { b++; } filtercount = b; while (fscanf(file1, "%79[^\n]\n", line) != eof) { (b = 0; b < filtercount; b++) { if (strstr(line,line2[b])) { fprintf(file3, "%s\n", line); break; } } } fclose(file1); fclose(file2); fclose(file3); return 0; }
however, input data seems specific enough make optimization assumptions. in particular, (a) files ordered first column value , (b) second reduced copy of first (in terms of line index values).
in case might want implement one-pass processing. scan both files line line comparing index values. advance next line of first file on every iteration. of second file, move next line when current line found in first file.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define check_file_open(file, name) \ if (file == null) { \ printf("failed open %s\n", name); \ return 1; \ } int main() { char line1[150] = ""; char line2[150] = ""; file *file1 = fopen("file1.csv", "r"); check_file_open(file1, "file1.csv"); file *file2 = fopen("file2.csv", "r"); check_file_open(file2, "file2.csv"); file *file3 = fopen("file3.csv", "w"); check_file_open(file3, "file3.csv"); bool eof1 = fscanf(file1, "%79[^\n]\n", line1) == eof; bool eof2 = fscanf(file2, "%79[^\n]\n", line2) == eof; while (!eof1 && !eof2) { if (strstr(line1, line2)) { fprintf(file3, "%s\n", line1); eof2 = fscanf(file2, "%79[^\n]\n", line2) == eof; } eof1 = fscanf(file1, "%79[^\n]\n", line1) == eof; } fclose(file1); fclose(file2); fclose(file3); return 0; }
Comments
Post a Comment