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. C language Help using Dirent.h

C language Help using Dirent.h

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
25 Posts 4 Posters 4 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 a random user

    you have been very helpfull to me and believe it or not im so close to finish the first exercise of the project as you adviced I will study the materials again but it will be after I will hand it out because I have left a few more hours to work on it till it hits 11:50 pm

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

    im sorry to bother you again but my time is not very short and I have no where else to go I combined the 2 codes i made into 1 but i keep getting now an error on the first function on the while How do i fix it? http://pastebin.com/p52qeXix[^]

    1 Reply Last reply
    0
    • A a random user

      well.. im trying to open a folder with an unknown number of programs using argv and argc through the cmd to the adress of the folder i need to use the dirent.h and maybe the FILE struct to open the files the code needs to open each file and search the text within (it has mp3 signatures etc) in a binary way and search through it with a second file that the text inside of it is not in any place in there in other words i need to spot if the second file's signature is nto repeatign anywhere on the other files this is the far that i got but im stuck can anyone help me?

      #include <stdio.h>
      #include <stdlib.h>
      #include "dirent.h"

      int main(int argc, char** argv){

      int count = 0, size = 0;
      int sizes\[3\];
      FILE \*log = fopen("C:\\\\Users\\\\win7\\\\Desktop\\\\1\\\\C2\_Mid\_Anti-Virus\_Project.zip\\\\AntiVirusLog.txt", "wt");
      DIR \*dir;
      struct dirent \*ent;
      int found = 0;
      
      /\* Open directory \*/
      
      dir = opendir(argv\[1\]);
      
      
      
      while ((ent = readdir(dir)))
      {
          count++;
      
      
          sizes\[0\] = strlen(argv\[1\]);
          sizes\[1\] = 2;
          sizes\[2\] = strlen(ent->d\_name);
          size += sizes\[0\] + sizes\[1\] + sizes\[2\] + 1;
      
          char\* string = (char\*)malloc(sizeof(char)\*size);
          \_flushall();
          strcpy(string,argv\[1\]);
          strcat(string, "\\\\");
          strcat(string, ent->d\_name);
      
          FILE\* youtube = fopen (string, "rb");
      
          if (youtube != NULL)
          {
              print("success \\n");
          }
      
          puts(ent->d\_name);
      }
      
      
      
      
      
      fclose(log);
      closedir(dir);
      
      
      
      return 0;
      

      }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #22

      a random user wrote:

      well.. im trying to open a folder with an unknown number of programs

      You mean files?

      a random user wrote:

      this is the far that i got but im stuck

      So what is your code (not) doing? Be specific, as "im stuck" is all sorts of vague.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      1 Reply Last reply
      0
      • A a random user

        thank you but atm im trying to figure out how to open the folder that contains the folder that I entered in argv because its a fodler inside a folder that contains files as you can see it doesnt work int count = 0, size = 0, totalDirs = 0,location = 0; int sizes[3],flag = 0; int secSpot = 0; int argvSize, spot = 0; char c; argvSize = strlen(argv[1]); for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { spot++; } } for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { if (secSpot != spot) { secSpot++; } else if ((secSpot == spot) && (flag == 0)) { location = i; flag++; } } } location--; char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*location); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); free(entrenceToTheFolderBefore);

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #23

        a random user wrote:

        thank you but atm im trying to figure out how to open the folder that contains the folder that I entered in argv

        Which would be that folder's parent. I'm not sure that is what you really want to do. :confused:

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • A a random user

          insteadof entering "\\" my teacher told me that I could use / to make it quicker my problem is tho that when I do puts i get garbage and not the output i wanted as a link outside of the argv folder

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #24

          a random user wrote:

          insteadof entering "\\" my teacher told me that I could use / to make it quicker

          Perhaps one of you did not understand the other. Whether you use one character or two, the difference in time cannot be measured.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          1 Reply Last reply
          0
          • A a random user

            thank you but atm im trying to figure out how to open the folder that contains the folder that I entered in argv because its a fodler inside a folder that contains files as you can see it doesnt work int count = 0, size = 0, totalDirs = 0,location = 0; int sizes[3],flag = 0; int secSpot = 0; int argvSize, spot = 0; char c; argvSize = strlen(argv[1]); for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { spot++; } } for (int i = 0; i < argvSize; i++) { if (argv[1][i] == '/') { if (secSpot != spot) { secSpot++; } else if ((secSpot == spot) && (flag == 0)) { location = i; flag++; } } } location--; char* entrenceToTheFolderBefore = (char*)malloc(sizeof(char)*location); for (int i = 0; i < location; i++) { entrenceToTheFolderBefore[i] = argv[1][i]; } puts(entrenceToTheFolderBefore); free(entrenceToTheFolderBefore);

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #25

            Nothing personal, but this code looks all kinds of convoluted. When iterating the items in the folder pointed to by argv[1], how are you determining if an item is a file or a folder? [edit] Upon further inspection, I now see that code in dirent.h is a wrapper for this. My bad. [/edit] I suspect you need to be using the _findfirst()/_findnext() pair, or _stat() at the very minimum. It's been ages since I've used the former, but I envision something like:

            void processFile( const char *pFile )
            {
            // however you need to handle 'pFile' would go here
            }

            //====================================================================

            void processFolder( const char *pFolder )
            {
            char *p = (char *) malloc(strlen(pFolder) + 5);
            strcpy(p, pFolder);
            strcat(p, "\\*.*");

            struct \_finddata\_t fileinfo; 
            
            intptr\_t handle = \_findfirst(p, &fileinfo);
            if (handle != -1)
            {
                do
                {
                    if (fileinfo.attrib & \_A\_SUBDIR)
                    {   
                        if (fileinfo.name\[0\] != '.')
                            processFolder(fileinfo.name);
                    }
                    else
                        processFile(fileinfo.name);
                    
                } while(\_findnext(handle, &fileinfo) == 0);
            
                \_findclose(handle);
            }
            
            free(p);
            

            }

            //====================================================================

            void main(int argc, char* argv[])
            {
            if (argc == 2)
            processFolder(argv[1]);
            }

            Obviously this does not solve your overall problem, but you can't bother with checking the contents of a file before you have successfully iterated a folder.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            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