List files in directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Netwatcher
    New Member
    • Sep 2008
    • 58

    #16
    Is there a way to solve my problem without using a template (for educational purpose)?
    The rawer the better(no assembly code lol),
    Even only general guidelines on how to solve this.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #17
      By without a template do you mean without using vector and string?

      Comment

      • Netwatcher
        New Member
        • Sep 2008
        • 58

        #18
        Yes

        Yep!
        Without using STL or any other templates.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #19
          I can think of 2 ways either use a dynamically allocated array of pointers.

          That is you allocate and array of pointers to char (say 10 to start off with). Each time you read a file name you allocate memory to hold that file name and assign it to the next pointer in the array. When you reach the 11th file then you reallocate you array of 10 pointers to be 16 pointers (I believe 1.6 is an efficient growth factor). You will also have to keep a count of how many file-names you have read and make sure you deallocate the memory correctly.

          You will need to use the function malloc, realloc and free for memory handling.

          The second option is a linked list (look it up on wikipedia). I linked list can just be grown as you read more file names, you just need adding nodes to the list. Again you will need to use malloc and free (but not realloc).

          Comment

          • Netwatcher
            New Member
            • Sep 2008
            • 58

            #20
            List

            Think a linked list is the way to go on this one.
            (In these situation python is so tempting)




            Thanks,
            Netwatcher.
            http://www.csref.info <-> code reference site.

            Comment

            • Netwatcher
              New Member
              • Sep 2008
              • 58

              #21
              Used an allocated array but with new(C++) instead of malloc(C)

              Tried malloc too, found that new is simply faster (though delete is slower than free, and there's no reallocation with new :p)

              Code:
              	
              char** pt;
              pt = new char*[50];
              unsigned n=0;
              for(/*noimportant*/;n++)
              {
              /*lala
              lala*/
              pt[n]= hFindFile.cFileName;
              }
              for (n=0;pt[n]!=NULL;n++)
              {
                  cout <<pt[n]<<', ';
              }
              delete [] pt;
              return 0;
              }

              Comment

              Working...