- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试编写模拟反病毒扫描的代码,它扫描 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);
}
最佳答案
您的代码主要有两个主要问题
第 1 点:在您的代码中,对于像这样的一系列调用
search_sign(argv[1], buff, f7);
您正在使用未初始化的 buff
。 buff
然后作为 search_sign()
的第二个参数传递,(被接受为 str
)再次用作搜索字符串在 strstr()
.
由于buff
是一个自动 局部变量,初始内容(值)是垃圾(不确定的)因此,当用作strstr 中的搜索键时()
,将调用 undefined behaviour .
第 2 点:也就是说,as my previous comment ,在进一步使用返回的文件指针之前,您应该始终检查 fopen()
调用是否成功。
关于c - fprintf 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30729237/
在我的 C++ 代码中有几个 fprintf 语句,我已将其用于调试。由于我可能会再次需要它们,所以我暂时不想将它们注释掉。但是,我需要快速执行程序,所以我想避免它们被打印出来,因为它们目前是这样的(
巧合的是,我发现 disp(fprintf()) 打印了 fprintf 的字符串加上它所具有的字符数。我知道,disp() 是多余的,但出于纯粹的好奇心,我想知道为什么它会打印字符数,因为这可能有一
我正在运行一些基准测试以找到用 C++ 将巨大数组写入文件的最有效方法(在 ASCII 中超过 1Go)。 所以我将 std::ofstream 与 fprintf 进行了比较(参见下面我使用的开关)
假设我有两个线程将某些内容(相对较长)打印到 stderr 或 stdout,这两个流的函数是线程安全的,因为它们永远不会“交错”字符?因此,例如,如果我有“Hello, World”,我将永远不会得
我的应用程序(用 C 语言编写)必须将 500 个字符串出队并将它们写入文本文件。我可以将每个字符串出列并使用单独的 fprintf 语句将每个字符串写入一个文件,或者我可以将所有出列的字符串连接成一
我是编程新手,一开始试图用链表构建堆栈。 但是在打印列表中包含的元素时,fprintf()不起作用。 这是我的代码: #include #include //define stack and fu
我正在尝试代表一款战斗卡牌游戏: typedef struct list_of_cards* p_loc; typedef struct game game_s; struct list_of_car
我正在尝试使用以下 C 代码打印出我传入的数组。它应该以十六进制格式输出文本,每行一个,打开文件没有问题。当我第一次写它时,我的工作没有问题,我打开了输出文件,我的数组就在那里。我换了fileOutN
我正在尝试代表一款战斗卡牌游戏: typedef struct list_of_cards* p_loc; typedef struct game game_s; struct list_of_car
在我的代码中,fprintf 通过返回在 STREAM 中写入的字节数成功返回,但在实际文件中,我写入的字符串不存在。 最佳答案 输出可能只是经过缓冲。尝试使用 close() 关闭文件或在流上调用
我正在测试 fprintf() 的用法,但它不起作用。当我第一次编写代码时,我忘记在 fprintf() 中添加 \n 并且它起作用了。但是,当我在“test 1 2”的开头添加 \n 时,它停止了工
我正在开发一个生成 FDF 文件以填写 Adobe PDF 表单的程序。目前,我有各种被调用的过程,在每个过程终止之前,它打开 FDF 文件,写入其特定数据,然后关闭文件。 我试图将用于将数据写入
假设我有一个随机变量 a=1.2400 ,我想用四位有效数字打印它,即 1.240。我该怎么做? fprintf('%0.4g',a) % drops rightmost zero fprintf('
我真的需要一些帮助,我已经为此花了 6 个小时,我觉得我已经尝试了我能做的一切。这是我需要做的: 我需要将星号金字塔打印到 C 语言中的 triangle2.txt 文件中。 我知道我里面的代码将创建
C - fprintf 没有写入文件,知道为什么吗? #include #include int main(void){ FILE* pfile=fopen("/home/user-vla
如何在文件末尾为 fprintf() 用户输入的文本创建一个新行?我现在的代码是这样的: #include #include int main() { int lines; int
我的代码当前正在消耗日志文件输出的字符,但我不知道确切原因。 FILE* theLog; char filename[150]; theLog = fopen(filename, "w"); fpri
我正在尝试显示以下内容 static const char MSG[] = "Test %ld\n"; static int COUNT = 5; fprintf(stdout, "%s", MSG,
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
在下面的代码中,我尝试从套接字读取并将结果存储在文件中。 实际发生的情况是,我的客户端向我的服务器发送了一个 file.html 的 GET 请求。我的服务器找到该文件并将其内容写入套接字。最后,我的
我是一名优秀的程序员,十分优秀!