Simple anti virus project (Argv FILE* dirent.h)
-
well after hours of work I finally managed to combine 2 scripts that i worked on The purpse of the code is to get an adress of the folder that includes different kind of files and the code needs to open the folder that is near it (2 folders in 1 folder) and compair each time 2 files 1 is the compairer and the second is on of the files that is in the path in argv[1] but every time after I combined the code and tried to fix it theres a triggered breakpoint If anyone could tell me how to solve the problems in my script I will be very greatfull the part where it doesnt work is at the while in the function at the end here is my code: http://pastebin.com/C5BYSNYK
-
well after hours of work I finally managed to combine 2 scripts that i worked on The purpse of the code is to get an adress of the folder that includes different kind of files and the code needs to open the folder that is near it (2 folders in 1 folder) and compair each time 2 files 1 is the compairer and the second is on of the files that is in the path in argv[1] but every time after I combined the code and tried to fix it theres a triggered breakpoint If anyone could tell me how to solve the problems in my script I will be very greatfull the part where it doesnt work is at the while in the function at the end here is my code: http://pastebin.com/C5BYSNYK
You still have lines of code of the form:
char* entrenceToTheFolderBefore = (char*)malloc(NULL);
which, as I explained will lead to problems: you will either get strange results, or a program crash. If you are going to store data in the buffer returned from
malloc
then you must allocate enough space for the data you are going to copy into it. Anything less will cause your program to overwite other variables. -
You still have lines of code of the form:
char* entrenceToTheFolderBefore = (char*)malloc(NULL);
which, as I explained will lead to problems: you will either get strange results, or a program crash. If you are going to store data in the buffer returned from
malloc
then you must allocate enough space for the data you are going to copy into it. Anything less will cause your program to overwite other variables.i cant allocate because it gives me garbage every time i add 1 or simply try to allocate can you rewrite it to make more sence ?
-
i cant allocate because it gives me garbage every time i add 1 or simply try to allocate can you rewrite it to make more sence ?
OK, let us say that you have a path ("C:\Users\Noname\Documents\Test") in argv[1], and you now want to inspect a subdirectory called "images":
int argvSize = strlen(argv[1]); // the number of characters in the base path
// calculate the space needed for the subdirectory as follows:
// the length of the base path (argv[1])
// plus the length of the new subdirectory
// plus one for the backslash separator in front of the new directory
// plus 1 for the trailing null character
//
int mallocSize = argvSize + strlen(subdir) + 1 + 1;
char* newPath = (char*)malloc(mallocSize);
strcpy(newPath, argv[1]); // copy the root path
strcat(newPath, "\\"); // add a single trailing backslash - note two \\ required here,
// as the first one is treated as the escape character
strcat(newPath, subdir); // append the new directory name at the end.
// The trailing null is appended automatically by strcat// newPath should now contain:
// C:\Users\Noname\Documents\Test\images -
OK, let us say that you have a path ("C:\Users\Noname\Documents\Test") in argv[1], and you now want to inspect a subdirectory called "images":
int argvSize = strlen(argv[1]); // the number of characters in the base path
// calculate the space needed for the subdirectory as follows:
// the length of the base path (argv[1])
// plus the length of the new subdirectory
// plus one for the backslash separator in front of the new directory
// plus 1 for the trailing null character
//
int mallocSize = argvSize + strlen(subdir) + 1 + 1;
char* newPath = (char*)malloc(mallocSize);
strcpy(newPath, argv[1]); // copy the root path
strcat(newPath, "\\"); // add a single trailing backslash - note two \\ required here,
// as the first one is treated as the escape character
strcat(newPath, subdir); // append the new directory name at the end.
// The trailing null is appended automatically by strcat// newPath should now contain:
// C:\Users\Noname\Documents\Test\imagesfor some odd reason it still adds me junk letters that 1 last null int temp3 = location + 1; char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*temp3); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); is there any way we could communicate like through skype ? i have only 2 hours left before the handout
-
for some odd reason it still adds me junk letters that 1 last null int temp3 = location + 1; char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*temp3); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); is there any way we could communicate like through skype ? i have only 2 hours left before the handout
Yes because you are using the count from
location
to do the copy, which is the length of the string, and so will not include the null character. Use the code sample I gave you which makes use of standard library functions that will ensure your copied data is correctly structured.a random user wrote:
is there any way we could communicate like through skype ?
Sorry, I do this in my own time and at my own speed. I will not be available much longer today.
-
Yes because you are using the count from
location
to do the copy, which is the length of the string, and so will not include the null character. Use the code sample I gave you which makes use of standard library functions that will ensure your copied data is correctly structured.a random user wrote:
is there any way we could communicate like through skype ?
Sorry, I do this in my own time and at my own speed. I will not be available much longer today.
Ok then.. I did what you told me but the results are still the same The problem is in the second while and in entrenceToTheFolderBefore 1 more bit as null gives me =@$&!% int temp3 = location + 1; char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*temp3); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); location2 = location; while (location2 != argvSize) { location2++; UnwantedName[countLoop] = argv[1][location2]; // problem countLoop++; } OtherDir = opendir(entrenceToTheFolderBefore); while (OtherEnt = readdir(OtherDir)) ////UnwatedName = C2_Mid_Anti-Virus_Project.zip { if ((strcmp(OtherEnt->d_name, UnwantedName) != 0) && (strcmp(OtherEnt->d_name, ".") != 0) && (strcmp(OtherEnt->d_name, "..") != 0)) //problem { strcpy(InfectedFolderPath, entrenceToTheFolderBefore); strcat(InfectedFolderPath, "/"); strcat(InfectedFolderPath, OtherEnt->d_name); puts(InfectedFolderPath); } } closedir(OtherDir);
-
Ok then.. I did what you told me but the results are still the same The problem is in the second while and in entrenceToTheFolderBefore 1 more bit as null gives me =@$&!% int temp3 = location + 1; char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*temp3); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); location2 = location; while (location2 != argvSize) { location2++; UnwantedName[countLoop] = argv[1][location2]; // problem countLoop++; } OtherDir = opendir(entrenceToTheFolderBefore); while (OtherEnt = readdir(OtherDir)) ////UnwatedName = C2_Mid_Anti-Virus_Project.zip { if ((strcmp(OtherEnt->d_name, UnwantedName) != 0) && (strcmp(OtherEnt->d_name, ".") != 0) && (strcmp(OtherEnt->d_name, "..") != 0)) //problem { strcpy(InfectedFolderPath, entrenceToTheFolderBefore); strcat(InfectedFolderPath, "/"); strcat(InfectedFolderPath, OtherEnt->d_name); puts(InfectedFolderPath); } } closedir(OtherDir);
a random user wrote:
I did what you told me but the results are still the same
That's because you did not do what I told you. You are still copying your path strings based on
strlen
rather thanstrlen
+1. And you seem to be creating too many variables, most of which are just duplicates of existing items. This may well be what is causing you confusion. -
a random user wrote:
I did what you told me but the results are still the same
That's because you did not do what I told you. You are still copying your path strings based on
strlen
rather thanstrlen
+1. And you seem to be creating too many variables, most of which are just duplicates of existing items. This may well be what is causing you confusion.i did try that but the extra byte always when i do puts() adds alot of random letters int temp4 = argvSize - location; //temp4 = 30 char* UnwantedName = (char*)malloc(sizeof(char)*temp4); int temp3 = location+1; //temp3 = 24 char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*temp3); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); // ---> C:/Users/win7/Desktop/1=(alotof ascii characters
-
i did try that but the extra byte always when i do puts() adds alot of random letters int temp4 = argvSize - location; //temp4 = 30 char* UnwantedName = (char*)malloc(sizeof(char)*temp4); int temp3 = location+1; //temp3 = 24 char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*temp3); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); // ---> C:/Users/win7/Desktop/1=(alotof ascii characters
-
That is because you keep doing the same thing wrong. You need to stop and think, and reread all my posts that explain how to do it properly. Especially look closely at the sample code I posted yesterday
i did do waht you told me i expended the string by 1 but that 1 byte is messing up my code even trying using strcat just makes it worse I stopped and read all of ur recent psots but i have nothign that comes in mind that could help me im sorry for giving you a hard time here but it is hard for me as it is would you please fix the code to show me what you mean?
-
i did do waht you told me i expended the string by 1 but that 1 byte is messing up my code even trying using strcat just makes it worse I stopped and read all of ur recent psots but i have nothign that comes in mind that could help me im sorry for giving you a hard time here but it is hard for me as it is would you please fix the code to show me what you mean?
-
Go back to http://www.codeproject.com/Messages/5063518/Re-Simple-anti-virus-project-Argv-FILE-dirent-h.aspx[^], and read it carefully. I cannot explain it more than that.
I did calculate the bytes and the characters as i needed it needs to be 24 bytes when at the last code i posted you can see that until i reach to the point that is /1 its 23 bytes the + 1 is the one i added so it will be the end of the string