【问题标题】:Having problems with fprintffprintf 有问题
【发布时间】:2015-08-24 02:31:57
【问题描述】:

我正在尝试编写一个模拟反病毒扫描的代码,它扫描 5 个特定文件,然后创建一个名为 AntiVirusLog.txt 的文件。在此文件中,它会写入结果,例如PSY.avi INFECTED。受感染文件是包含文件youtubesign 中的字符串的文件。

我的问题是,当我尝试将结果打印到文件 AntiVirusLog.txt 时,它不会打印任何内容并将文件留空。

我的代码:

#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);
}

【问题讨论】:

  • 为什么你没有在main() 中检查fopen()
  • wt 修饰符对fopen 有什么作用?我想我以前没见过……
  • 除了其他人说的:打开文件然后关闭它是没有意义的,所以你不需要f = fopen(argv[1], "rb");fclose(f);(对于f2,f3,f4, f5, f6 但不是 f7)。此外,您的程序永远不会关闭 f7。
  • @AndreasGrapentin 这是为了指定它是一个文本文件。
  • msdn.microsoft.com/en-us/library/yeby3zcb.aspx 说你是对的。有趣的。这绝对是不标准的。

标签: c file-io printf


【解决方案1】:

我听了一些建议,并找到了自己的一些修复方法,现在它可以完美运行了!更新后的代码如下所示:

#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);
}

【讨论】:

  • @chux 你说的对,我忘记加了,谢谢!已编辑。
  • 您仍然没有检查 fopenf7f 是否没有失败
【解决方案2】:

您的代码主要有两个主要问题

  • 第 1 点:在您的代码中,对于类似的一系列调用

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

    您正在使用未初始化的buff。然后将buff 作为search_sign() 的第二个参数(被接受为str)传递,该参数再次用作strstr() 中的搜索字符串。

    由于buff 是一个自动 局部变量,初始内容(值)是垃圾(不确定),因此,当用作strstr() 中的搜索键时,将调用undefined behaviour .

  • 第 2 点:也就是说,as my previous comment,在进一步使用返回的文件指针之前,您应该始终检查 fopen() 调用是否成功。

【讨论】:

  • 一个有效点,但可能与所描述的行为无关,除非它在写入任何内容之前在strstr 中崩溃。
  • @AndreasGrapentin 好吧,也许但 UB 就是 UB,没有具体说明,没有什么是确定的。
  • @AndreasGrapentin 如果您想指出fopen() 检查,请注意我之前对答案本身的评论。 :-)
  • 我知道你的观点是有效的(我赞成),我只是不认为这是 OP 在这种情况下描述的问题的根本原因。我知道他的电脑现在可能正在生出霓虹粉色的独角兽,但在这种情况下strstr 更有可能只返回NULL
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 2017-03-01
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多