Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Simple anti virus project (Argv FILE* dirent.h)

Simple anti virus project (Argv FILE* dirent.h)

Scheduled Pinned Locked Moved C / C++ / MFC
comdebuggingtoolshelptutorial
13 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    a random user
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • A a random user

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • L Lost User

        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.

        A Offline
        A Offline
        a random user
        wrote on last edited by
        #3

        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 ?

        L 1 Reply Last reply
        0
        • A a random user

          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 ?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • L Lost User

            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

            A Offline
            A Offline
            a random user
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • A a random user

              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

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              A 1 Reply Last reply
              0
              • L Lost User

                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.

                A Offline
                A Offline
                a random user
                wrote on last edited by
                #7

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

                L 1 Reply Last reply
                0
                • A a random user

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

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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 than strlen+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 1 Reply Last reply
                  0
                  • L Lost User

                    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 than strlen+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 Offline
                    A Offline
                    a random user
                    wrote on last edited by
                    #9

                    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

                    L 1 Reply Last reply
                    0
                    • A a random user

                      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

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      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

                      A 1 Reply Last reply
                      0
                      • L Lost User

                        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

                        A Offline
                        A Offline
                        a random user
                        wrote on last edited by
                        #11

                        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?

                        L 1 Reply Last reply
                        0
                        • A a random user

                          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?

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          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.

                          A 1 Reply Last reply
                          0
                          • L Lost User

                            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.

                            A Offline
                            A Offline
                            a random user
                            wrote on last edited by
                            #13

                            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

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups