the code checks if string is in directory files -the code doesnt find matches..i'm using dirent.h

40 views Asked by At

The code checks if a string is in directory files, I think I have problem with
the my searchFile() function

  • argv1 is the folder
  • argv2 is the string to search
  • argv3 is the result file

            #include "dirent.h"
            #include<stdio.h>
            #include<stdlib.h>
            int searchFile(FILE* File1, char * buffer);
            void saveFile(int flag, char * File1Name, char* logAddress);
    
            int main(int argc, char **argv) {
                int flag = 0;
                DIR* dir;
                FILE* File1;
                char * buffer=0;
                long length;
                char address[1000];
                struct dirent *pDirent;
                    dir = opendir(argv[1]);
                    if (dir == NULL) 
                    {
                        printf("Cannot open directory '%s'\n", argv[1]);
                        flag=1;
                    }
                    //convert the signeture file to string
                    FILE * f = fopen(argv[2], "rb");
                    if (f)
                    {
                        fseek(f, 0, SEEK_END);
                        length = ftell(f);
                        fseek(f, 0, SEEK_SET);
                        buffer = (char*)malloc(length*sizeof(char));
                        if (buffer)
                        {
                            fread(buffer, 1, length, f);
                        }
                        fclose(f);
                    }
    
                    if (buffer)
                    {
                        strcpy(address, argv[1]);
                        while ((pDirent = readdir(dir)) != NULL)
                        {
                            if (pDirent->d_name[0] != '.')
                            {
                                printf("[%s]\n", pDirent->d_name);
                                strcat(address, pDirent->d_name);
                                File1 = fopen(address, "rb");
                                printf("%s.\n", address);
                                if (!File1) printf("PROBLEM.\n");
                                flag = searchFile(File1, buffer);
                                saveFile(flag, pDirent->d_name,argv[3]);
                                strcpy(address, argv[1]); 
                            }
    
                        }
                        closedir(dir);
                    }
                    free(buffer);
                system("PAUSE");
                return (0);
            }
    

Input:the file in the dir , FILE*

The function search if a string is in the file and send the result to save file function

Output:none

            int searchFile(FILE* File1, char * buffer)
            {
                int char1, flag = 0,i=0;
                char temp[1000];

                while ((char1 = fgetc(File1)) != EOF && flag == 0)
                {
                    if (*(buffer + i) == char1)
                    {
                        i++;
                        if (strlen(buffer) == i)//if the whole string is             in the FILE.
                        {
                            flag = 1;
                        }
                    }
                    else
                    {
                        i = 0;

                    }
                }
                fclose(File1);
                return flag;
            }

Input:the file in the dir , FILE* int flag

The function prints the all the files and tells who is infected and who isn't

            void saveFile(int flag, char * File1Name, char *logAddress)
            {
                FILE* File2 = fopen(logAddress, "a+");
                if (File2)
                {
                    fprintf(File2, File1Name);
                    if (flag == 1)
                    {
                        fprintf(File2, "  INFECTED!\n");
                    }
                    else
                    {
                        fprintf(File2, "  not infected\n");
                    }
                }
                fclose(File2);
            }
0

There are 0 answers