Hello I'm working on this code for the school and we are generating emails for freshman students, however, the system probably due to server overload fails to generate some emails. So I'm working on this c code( probably was a wrong choice) to search for regexp matching the generated emails and deleting from the list of students and propective emails.
I"m currently at the stage of making sure the code can find all the matching regexp from the student list file before deleting!
But the code fails to read all matching regexp?
Code:
//Filename: SearchReplace.c
//Usage: This searches for lines in a file containing a particular work and deletes the lines.
//Licence: GNU P.L.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BUF_SIZE 1000
char buffer[BUF_SIZE];
int read_str(char *s, FILE *fp) {
int i = 0;
char c;
for( i = 0; i < 30;i++)
s[i] = ' ';
i = 0;
while(( c = fgetc(fp)) != ',') {
if(c == EOF)
return -1;
if(!isspace(c) && c != '\n')
s[i++] = c;
}
s[i] = '\0';
printf("\nRead String: %s\n\n", s);
return 1;
}
int main(int argc, char *argv[]) {
//reads the strings from an input file
FILE *fp = fopen(argv[1],"r");
if( fp == NULL) {
fprintf(stderr,"\nInvalid Input file.");
exit(EXIT_SUCCESS);
}
FILE *file = fopen(argv[2], "r+");
if( file == NULL) {
fprintf(stderr,"\nInvalid CSV file.");
exit(EXIT_SUCCESS);
}
//writes the output to this file.
char str[30];
int i = 0;
while( read_str(str,fp) != -1) {
//searches CSV file and prints the lines with no matches to the same file.
for( i = 0; fgets(buffer, sizeof(buffer), file);i++){
printf("\n Buffer: %s", buffer);
printf("\n String: %s", str);
if( strcasestr(buffer, str) == 1){
//writes matching strings.
printf("String: %s, Buffer: %s\n",str, buffer);
}
}
i = 0;
rewind(file);//resets file pointer to beginning.
}
fclose(fp);
fclose(file);
return 0;
}
Hope someone could give me a hint on what i'm missing. It recognises some regexp and fails to recognise some. Been staring for hours!