Thread: dir output problems

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    dir output problems

    hey, i'm exploring the Accessing a directory and all the files within it example in the faq for win32, and i've found a way to output the filenames to a file.
    Code:
    // print out in sorted order
      int numdirs = 0;
      for (i = 0; i < list.num_entries; i++)
      {
        char  t1[50], t2[50], t3[50], a[10];
        format_time(&list.files[i].ftCreationTime, t1);
        format_time(&list.files[i].ftLastAccessTime, t2);
        format_time(&list.files[i].ftLastWriteTime, t3);
        format_attr(list.files[i].dwFileAttributes, a);
        if (list.files[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
          // 'null' date for directory access times, which change each time
          // we run this tool
          sprintf(t2, "%4d/%02d/%02d %02d:%02d:%02d", 2000, 1, 1, 0, 0, 0);
        }
        
        
            fprintf(fileptr, "%s\n", list.files[i].cFileName);
    
        printf("%s %10ld %s %s %s %s\\%s\n", a, list.files[i].nFileSizeLow, t1, t2, t3, root, list.files[i].cFileName);
        if (list.files[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) numdirs++;
      }
    problem is, if theres multiple dir's, it creates the file in each dir and outputs the content to that file. what i want it to do, is output all of the files to the txt file in the root dir. similar to the dos command: dir /s /b > output.txt

    any help is welcome :] thx
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fileptr
    Pass this as a parameter, which is fopen() and fclose() in the main function.

    void doit(char *root, FILE *fileptr);

    doit(newroot,fileptr);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    works like a charm

    now the only problem i have is telling which file(s) belong in which directory(/ies). it outputs the folder name(s), and then the filename(s) of the file(s) in the current folder, and then the filename(s) of the file(s) in the next folder deep, etc. also if there's two files with the same name, one in the root dir and one in a sub dir, it only outputs the filename once. is there a way i could add like \subdir\ to filename.ext before it gets output?

    thanks



    // also, the program isn't perfect. i created 3 directories, each with a file in the directory and it only lists those files in the first directory. i modified the doit() function to accept another string to output as to which dir it's in.
    Code:
    void doit(char *root, FILE *fileptr, char *sub)
    {     
      flist           list = { 0, 0, NULL };
      HANDLE          h;
      WIN32_FIND_DATA info;
      int             i;
    
      // build a list of files
      h = FindFirstFile("*.*", &info);
      if (h != INVALID_HANDLE_VALUE)
      {
        do
        {
          if (!(strcmp(info.cFileName, ".") == 0 || strcmp(info.cFileName, "..") == 0))
          {
            addfile(&list, info);
          }
        } while (FindNextFile(h, &info));
        if (GetLastError() != ERROR_NO_MORE_FILES) errormessage();
        FindClose(h);
      }
      else
      {
        errormessage();
      }
    
      // sort them
      qsort(list.files, list.num_entries, sizeof(list.files[0]), sortfiles);
    
      // print out in sorted order
      int numdirs = 0;
      for (i = 0; i < list.num_entries; i++)
      {
        char  t1[50], t2[50], t3[50], a[10];
        format_time(&list.files[i].ftCreationTime, t1);
        format_time(&list.files[i].ftLastAccessTime, t2);
        format_time(&list.files[i].ftLastWriteTime, t3);
        format_attr(list.files[i].dwFileAttributes, a);
        if (list.files[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
          // 'null' date for directory access times, which change each time
          // we run this tool
          sprintf(t2, "%4d/%02d/%02d %02d:%02d:%02d", 2000, 1, 1, 0, 0, 0);
        }
        
            fprintf(fileptr, "%s", sub);
            fprintf(fileptr, "%s\n", list.files[i].cFileName);
    
        printf("%s %10ld %s %s %s %s\\%s\n", a, list.files[i].nFileSizeLow, t1, t2, t3, root, list.files[i].cFileName);
        if (list.files[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) numdirs++;
      }
    
      // now process all the sub-dirs
      // free all the files first, to save a bit of space
      // the sort function will have put them all at the end.
      list.files = (WIN32_FIND_DATA *) realloc(list.files, numdirs * sizeof(WIN32_FIND_DATA));
      for (i = 0; i < numdirs; i++)
      {
        char  newroot[MAX_PATH];
        char sub[100] = ".\\";
        sprintf(newroot, "%s\\%s", root, list.files[i].cFileName);
        SetCurrentDirectory(list.files[i].cFileName);
        strcat(sub, list.files[i].cFileName);
        strcat(sub, "\\");
        doit(newroot, fileptr, sub);
        SetCurrentDirectory("..");
      }
    
      // free the remainder
      free(list.files);
      
      fclose(fileptr);
    }
    program output:
    .\ay
    .\nameoffolder
    .\shibby
    .\anotherfile.txt
    .\dirlist.txt
    .\xtree.cpp
    .\xtree.exe
    .\ay\lukeiamyourfather.txt
    output of dir /s /b
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\anotherfile.txt
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\ay
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\dirlist.txt
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\nameoffolder
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\output.txt
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\shibby
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\xtree.cpp
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\xtree.exe
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\ay\lukeiamyourfather.txt
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\nameoffolder\yousuck.txt
    C:\Documents and Settings\Tim Hansen\My Documents\cpp\shibby\POS.txt
    lol slight difference..
    Last edited by willc0de4food; 04-17-2006 at 07:15 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you have two 'sub' in scope.
    You need to append the new sub-directory onto the end of the parameter sub, not create a new one from scratch.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have a few minor problems trying to finish this program
    By kisiellll in forum C Programming
    Replies: 4
    Last Post: 02-22-2009, 07:00 PM
  2. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  3. why this output?
    By dredre in forum C Programming
    Replies: 4
    Last Post: 05-08-2004, 04:09 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. input output problems
    By gell10 in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2003, 09:39 PM