I am trying to create an antivirus software, in order to learn how file streams work in c. The error occurs in the function read_file();
, in which windows sends an error messages saying that the executable is not responding, and then the program terminates execution...
Code::Blocks soon says at the end of the program execution something similar to the following:
Process returned -1073741819 (0xC0000005) execution time : 3.756 s
Press any key to continue...
I am susupecting that this is a memory error, in which too much memory is being allocated, however I am not sure.
Here is the code:
/*
*Name: ZKjm310
*Objective: Create an antivirus, by comparing file hashes
*Date: 2/3/18
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
//#include "sha256.h" // separate header file for creating sha256 hashes
//file path for virus definitions
#define PATH "C:\\Users\\matth\\Box Sync\\Programmming\\Workspaces\\Personal\\C+CPP Projects\\VirusAnti\\Test.txt"
char* read_defs()
{
char* virus_defs = (char*)malloc(sizeof(char) * 1000000); // allocates memory to store virus definitions
FILE *fp; // main virus definitions file
fp = fopen(PATH, "rb");
if(fp == NULL)
{
printf("[ERROR] Could not find virus definitions! :\'\(");
pause();
free(virus_defs);
exit(1);
}
int i = 0; // iterator, iterates through virus_defs, for adding definitions
while(!feof(fp))
{
// reads virus definitions from file
char read_memory;
read_memory = fgetc(fp);
virus_defs[i] = read_memory;
i++;
}
fclose(fp);
return virus_defs; // returns read virus definitions
}
void pause()
{
// pauses program by asking "Press <enter> to continue..." and user presses enter
char x[2];
printf("\nPress <enter> to continue...\n");
fgets(x, sizeof(x), stdin);
}
char* read_file(char* fname)
{
// reads file and returns char* for file content
FILE *file;
char* file_content = (char*)malloc(sizeof(char) * 100000000);
file = fopen(fname, "rb");
if(file == NULL)
{
return NULL;
}
int i = 0;
while(!feof(file));
{
char read_memory;
read_memory = fgetc(file);
file_content[i] = read_memory;
i++;
}
fclose(file);
return file_content;
}
int main(int argc, char* argv[])
{
bool isRunning = true;
printf("[INFO] Loading virus definitions");
char* virus_defs = read_defs();
printf("\n[INFO] Virus definitions loaded!\n");
printf("[INFO] Getting ready to scan files...\n");
printf("[INFO] Virus Definitions:\n%s\n", virus_defs);
while(isRunning)
{
char _fname[500];
printf("Enter file to scan:\n");
fgets(_fname, sizeof(_fname), stdin);
const char* fname = _fname;
char* file_read = read_file(fname);
printf("File content read:\n%s\n", file_read);
}
return 0;
}
Please Note: The application is not fully completed, and I am currently working on the reading and the hashing of some files. This program is only to be created in order to understand file streams in c, and will not be used for commercial use.