Thread: Reading Matching strings from a file and deleting from another.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    Reading Matching strings from a file and deleting from another.

    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!
    Last edited by Nyah Check; 09-26-2014 at 10:54 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Indent style - Wikipedia, the free encyclopedia
    Use this knowledge to present us with readable code.

    > It recognises some regexp and fails to recognise some. Been staring for hours!
    strcasestr doesn't do regular expressions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    @Salem corrected the indent prob.

    I was thinking strcasestr searches for substrings ignoring the cases of both args?

    So what does strcasestr do?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Nyah Check View Post
    So what does strcasestr do?
    Hmm...let me Google that for you...or not. I would hate to rob you of some (apparently) much-needed searching practice.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
        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){
    I suggest you read the manual page for strcasestr to find out what it actually returns.
    strcasestr(3): locate substring - Linux man page
    For example, this version returns a pointer, so comparing the result with 1 is just not going to work.

    Remember, things like strcmp() return 0 on success, not 1.

    > So what does strcasestr do?
    I'm dismayed that you even need to ask.
    RTFM before using a function if you're in ANY doubt at all as to how it is supposed to work.
    strcasestr isn't even a standard function, so it's anyone's guess as to what it does on YOUR platform.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pattern matching in strings
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 06:30 AM
  2. matching of names in 2d array of strings...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-25-2009, 09:59 AM
  3. Help with reading strings from a file
    By ldcel in forum C Programming
    Replies: 3
    Last Post: 12-01-2007, 01:31 AM
  4. matching strings at end
    By kurz7 in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 07:27 AM
  5. Reading strings from a file
    By Capulet in forum C Programming
    Replies: 7
    Last Post: 12-04-2002, 04:58 AM