0

I need to clear up non-active users from a directory and for that purpose I am creating the following function.

I currently have two lists: usernames (file1) and email (file2) addresses on individual lines. I need to find if the username from file1 exists in the email address list from file2.

These are the steps I have considered:

  1. Read the first username from file1 and remove the newline character.
  2. Start reading the email addresses from the file2, split at the "@" symbol and compare.
  3. If compare is succeeded, insert "##" sign in front of the username in the file1. (How can this be achieved?)

Problem

(Resolved by @MC93 answer)I am currently stuck at step 2. My program is only comparing the first username from the file1 and then stops comparing. Program exists normally.

Current Issue is Step3 and Improvements!!

Also, should I read the file2 split the words and store them in a sorted balanced tree to improve perfomance. If not, any other suggestions.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ( int argc, char** argv )
{
    FILE * file1;
    FILE * file2;
    char * lineFromFile1 = NULL;
    char * lineFromFile2 = NULL;
    size_t len = 0;
    ssize_t read1, read2;
    char * token;
    char * search = "@";

    file1 = fopen("username.txt", "r");
    file2 = fopen("email.txt", "r");   
    if ( file1 == NULL || file2 == NULL )
    {
        exit(EXIT_FAILURE);
    }

    while ((read1 = getline(&lineFromFile1, &len, file1)) != -1)
    {
        // Removing the newline character
        if (lineFromFile1[strlen(lineFromFile1)-1] == '\n')
        {
            lineFromFile1[strlen(lineFromFile1)-1] = '\0';
        }
        printf("\nCurrent Username: %s \n", lineFromFile1);

        // Reading email addresses and comparing
        while ((read2 = getline(&lineFromFile2, &len, file2)) != -1)
        {
            // Splitting string at the '@' sign
            token = strtok(lineFromFile2, search);

            // Comparing strings
            if ( strcmp(lineFromFile1, token) == 0)
            {
                printf("%s from File1 exists in File2 \n", lineFromFile1);
            }

            token = strtok(NULL, lineFromFile2);
            token = NULL;
        }
        rewind(file2);
    }

    fclose(file1);
    fclose(file2);
    if ( lineFromFile1 || lineFromFile2 || token)
    {
        free(lineFromFile1);
        free(lineFromFile2);
        free(token);
    }
}

File Contents

File1                            File2
username                         email
janedoe                          johndoe@google.com
johndoe                          janedoe@google.com

Current Username: janedoe 
janedoe from File1 exists in File2 

Current Username: johndoe 

RUN FINISHED; exit value 0; real time: 10ms; user: 0ms; system: 0ms

1 Answers1

4

You read the entirety of File2 in the second while-loop, but you need to then reset the internal pointer in File2 back to the beginning of the file for subsequent searches, which you can do by adding this line after the second while-loop:

rewind(file2);

or if you prefer:

fseek(file2, 0, SEEK_SET);
MC93
  • 791
  • 6
  • 14
  • This works. Can you suggest improvement in the program and also how can I achieve the Step3? – Boring Loop Mar 03 '16 at 10:58
  • The accepted answer here shows how you can read and write to the same file using `fseek` and `ftell`: http://stackoverflow.com/questions/5787867/file-read-write-to-same-file – MC93 Mar 03 '16 at 11:51
  • Actually, what you need is to add to the file without overwriting it, and it looks like you can't really do that all in one, so you should probably create a temporary file to which write file1 contents with added "##"s and then overwrite the original and delete the temporary at the end: http://stackoverflow.com/questions/9033060/c-function-to-insert-text-at-particular-location-in-file-without-over-writing-th – MC93 Mar 03 '16 at 12:17
  • BTW, since my answer worked for you, would you please consider accepting it? – MC93 Mar 03 '16 at 12:20
  • can you guide me how to sort a string field in a struct (only 1 field)? – Boring Loop Mar 03 '16 at 12:56