Having problems with fprintf

272 Views Asked by At

I am trying to write a code that simulates an Anti-Virus scan, it scans 5 specific files and then creates a file named AntiVirusLog.txt. In this file it writes the results, for example PSY.avi INFECTED. An infected file is a file that contains the string in the file youtubesign.

My problem is when I try to print in the results to the file AntiVirusLog.txt it does not print anything and leaves the file blank.

My code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<dirent.h>
#define BUZZ_SIZE 1024
int fast_scan(char *fname, char *str, FILE *fs);
int slow_scan(char *fname, char *str, FILE *fs);
int main(int argc, char *argv[])
{
    char name[100];
    char choice[5];
    char buff[BUZZ_SIZE];
    FILE *f7, *f2;
    struct dirent *de;
    DIR *dr = opendir(argv[1]);
    if (dr == NULL)  // opendir returns NULL if couldn't open directory
    {
        printf("Could not open current directory");
        return 0;
    }
    f2 = fopen(argv[2], "rb");
    f7 = fopen("AntiVirusLog.txt", "wt");
    printf("Welcome to Amnon's Anti-Virus program\n which scan would you like to choose?:\n");
    printf("Fast: check only the first and the last 20% of the file\n Slow: Checks the entire file\n");
    printf("Enter fast for a fast scan and slow for a slow scan\n");
    scanf("%s", choice);
    if ((strcmp(choice, "slow"))==0)
    {
        while ((de = readdir(dr)) != NULL)
        {
            strcpy(name, argv[1]);
            strcat(name, de->d_name);
            if((fgets(buff, BUZZ_SIZE, f2)) != NULL)
            {
                slow_scan(name, buff, f7);
            }
        }
    }
    if ((strcmp(choice, "fast")) == 0)
    {
        while ((de = readdir(dr)) != NULL)
        {
            strcpy(name, argv[1]);
            strcat(name, de->d_name);
            if ((fgets(buff, BUZZ_SIZE, f2)) != NULL)
            {
                fast_scan(name, buff, f7);
            }
        }
    }
    printf("The scan was made successfuly, check the file AntiVirusLog.txt to see the results\n");
    closedir(dr);
    fclose(f2);
    fclose(f7);
    system("PAUSE");
    return (0);
}
int slow_scan(char *fname, char *str, FILE *fs)
{
    int findres = 0;
    FILE *fp;
    char temp[BUZZ_SIZE];
    if ((fopen_s(&fp, fname, "rb")) != NULL)
    {
        return(-1);
    }
    while ((fgets(temp, BUZZ_SIZE, fp)) != NULL)
    {
        if ((strstr(temp, str)) != NULL)
        {
            fprintf(fs, "%s INFECTED\n", fname);
            findres++;
        }
    }
    if (findres==0)
    {
        fprintf(fs, "%s NOT INFECTED\n", fname);
    }
    fclose(fp);
    return(0);
}
int fast_scan(char *fname, char *str, FILE *fs)
{
    int findres=0;
    int i, j, len, partlen;
    FILE *fp;
    if ((fopen_s(&fp, fname, "rb")) != NULL)
    {
        return(-1);
    }
    fseek(fp, 0, SEEK_END);
    len = ftell(fp);
    partlen = (len * 20) / 100;
    char *temp=malloc(partlen);
    while ((fgets(temp, BUZZ_SIZE, fp)) != NULL)
    {
        for (i = 0; i < partlen; i++)
        {
            if (temp[i]=str[i])
            {
                findres++;
            }
            if (temp[i] != str[i])
            {
                i = partlen + 1;
            }
            if (findres == partlen)
            {
                fprintf(fs, "%s INFECTED\n", fname);
                i = partlen + 1;
            }
        }
        for (j = len - partlen; j < len; j++)
        {
            if (temp[j] = str[j])
            {
                findres++;
            }
            if (temp[j] != str[j])
            {
                j = partlen + 1;
            }
            if (findres == partlen)
            {
                fprintf(fs, "%s INFECTED\n", fname);
                j = partlen + 1;
            }
        }
    }
    if (findres!= partlen)
    {
        fprintf(fs, "%s NOT INFECTED\n", fname);
    }
    fclose(fp);
    return(0);
}
2

There are 2 best solutions below

2
On

I have used some of the advices listen and found some fixes of my own and now it works perfectly! the updated code looks like this:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUZZ_SIZE 1024
int search_sign(char *fname, char *str, FILE *fs);
int main(int argc, char *argv[])
{
    char buff[BUZZ_SIZE];
    FILE *f,*f7;
    f7 = fopen("AntiVirusLog.txt", "wt");
    f = fopen(argv[1], "rb");
    if ((fgets(buff, BUZZ_SIZE, f)) != NULL)
    {
        search_sign(argv[2], buff, f7);
        search_sign(argv[3], buff, f7);
        search_sign(argv[4], buff, f7);
        search_sign(argv[5], buff, f7);
        search_sign(argv[6], buff, f7);
    }
    printf("The scan was made successfuly, check the file AntiVirusLog.txt to see the results\n");
    fclose(f);
    fclose(f7);
    system("PAUSE");
    return (0);
}
int search_sign(char *fname, char *str, FILE *fs)
{
    int findres = 0;
    FILE *fp;
    char temp[BUZZ_SIZE];
    if ((fopen_s(&fp, fname, "rb")) != NULL)
    {
        return(-1);
    }
    while ((fgets(temp, BUZZ_SIZE, fp)) != NULL)
    {
        if ((strstr(temp, str)) != NULL)
        {
            fprintf(fs, "%s INFECTED\n", fname);
            findres++;
        }
    }
    if (findres==0)
    {
        fprintf(fs, "%s NOT INFECTED\n", fname);
    }
    fclose(fp);
    return(0);
}
4
On

There are primarily two major issues with your code

  • Point 1: In your code, for the series of calls like

     search_sign(argv[1], buff, f7);
    

    you're using buff uninitialized. The buff is then passed as the second parameter of search_sign(), (to be accepted as str) which is again used as the search string in strstr().

    As buff is an automatic local variable, the initial content (value) is garbage (indeterminate) and hence , when used as the search key in strstr(), will invoke undefined behaviour.

  • Point 2: That said, as my previous comment, you should always be checking the success of fopen() call(s) before using the returned file pointer any further.