Menu

Reading a c:\directory with C++

2002-09-10
2012-09-26
  • Nobody/Anonymous

    Hi. Do any of guys hava a small example on how to read a directory with C++?

    BR Kurt

     
    • Nobody/Anonymous

      No, but I do know how to read it using the win32 api:FindFirstFile, FindNextFile. I use it to read a whole CD into a database. Too long code to paste here.

       
    • Nobody/Anonymous

      Try this,

      #include <stdio.h>
      #include <conio.c>
      #include <conio.h>
      #include <iostream.h>
      #include <fstream.h>
      #include <string>

      void read()

      {

      ifstream OpenFile("c:\\your file.txt");

      char ch;

      while(!OpenFile.eof())

      {

      OpenFile.get(ch);

      cout << ch;

      }

      OpenFile.close();
      getche();

      }

       
    • Nobody/Anonymous

      Well the solution #include <stdio.h>
      #include <conio.c>
      #include <conio.h>
      #include <iostream.h>
      #include <fstream.h>
      #include <string>

      void read()

      {

      ifstream OpenFile("c:\\your file.txt");

      char ch;

      while(!OpenFile.eof())

      {

      OpenFile.get(ch);

      cout << ch;

      }

      OpenFile.close();
      getche();

      }

      Just reads a character from a file. I want to read the filenames from the harddesk.

       
    • Nobody/Anonymous

      Ok, I'll try to paste some code here, let's see what happens, I hope the sourceforge server does not reboot.
      It's part of my program, you should remove some stuff from it (like Berkely DB inserts). It's complicated, because dir names and files names must be separated.

      BOOL fnImportFiles(char *aktualis)
      {

      HANDLE          FindHandle;
      WIN32_FIND_DATA FindData;
      char            DirectoryName[MAX_PATH];
      char            DirNameW1[MAX_PATH];
      char            DirNameW2[MAX_PATH];
      char datawork[50]="";
      char keywork[255];
      DBT key, data;
      int ret;
      memset(&key, 0, sizeof(key));
      memset(&data, 0, sizeof(data));
      key.size = 255; 
      data.data = datawork;
      data.size = 50;

      //  Set the new current directory

      SetCurrentDirectory( aktualis );

      FindHandle = FindFirstFile( "*.*", &FindData );
      //    If it is a real directory
      if (( FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) &&
            ( strcmp(FindData.cFileName, "..") && strcmp(FindData.cFileName, ".") ) )
      {
            //       Get the current directory
            GetCurrentDirectory( MAX_PATH, DirectoryName );
            //       Put a "\&quot; if necessary
            if ( strncmp( &DirectoryName[strlen(DirectoryName)-1], "\\&quot;, 1 ) )
                 (void) strcat( DirectoryName, "\\&quot; );

            //       Create a new path
            (void) strcat( DirectoryName, FindData.cFileName );
            fnImportFiles(DirectoryName);

            //       Go back one level
            SetCurrentDirectory( ".." );
      }
      else
      {
            if (( strcmp(FindData.cFileName,"..")==0)||( strcmp(FindData.cFileName,".")==0))
                  ;
            else
            {
                  GetCurrentDirectory( MAX_PATH, DirNameW1 );
                  if ( strncmp( &DirNameW1[strlen(DirNameW1)-1], "\\&quot;, 1 ) )
                      (void) strcat( DirNameW1, "\\&quot; );
                  strcpy(DirNameW2, szVolume);
                  strcat(DirNameW2, strstr(DirNameW1, ":"));
                  strcat(DirNameW2, FindData.cFileName);
                  strncpy(keywork, strlwr(DirNameW2), 255);
                  key.data = keywork;
                     if ((ret = hdb4->put(hdb4, NULL, &key, &data, DB_NOOVERWRITE)) == 0)
                          ;  
                  else
                          return FALSE;
         
            }
      }

      //  loop to get all inside the current directory
      while ( FindNextFile( FindHandle, &FindData ) )
      {
              //    If it is a real directory
              if (( FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) &&
                          ( strcmp(FindData.cFileName, "..") && strcmp(FindData.cFileName, ".") ) )
                 {
                  //       Get the current directory
                  GetCurrentDirectory( MAX_PATH, DirectoryName );

                  //       Put a "\&quot; if necessary
                  if ( strncmp( &DirectoryName[strlen(DirectoryName)-1], "\\&quot;, 1 ) )
                  (void) strcat( DirectoryName, "\\&quot; );

                  //       Create a new path
                  (void) strcat( DirectoryName, FindData.cFileName );
                  //       Make a new call to itself
                  fnImportFiles( DirectoryName );

                  //       Go back one level
                  SetCurrentDirectory( ".." );

              }
              else
              {
                  if (( strcmp(FindData.cFileName,"..")==0)||( strcmp(FindData.cFileName,".")==0))
                      ;
                  else
                  {
                      GetCurrentDirectory( MAX_PATH, DirNameW1 );
                      if ( strncmp( &DirNameW1[strlen(DirNameW1)-1], "\\&quot;, 1 ) )
                          (void) strcat( DirNameW1, "\\&quot; );
                      strcpy(DirNameW2, szVolume);
                      strcat(DirNameW2, strstr(DirNameW1, ":"));
                      strcat(DirNameW2, FindData.cFileName);
                      strncpy(keywork, strlwr(DirNameW2), 255);
                      key.data = keywork;
                      if ((ret = hdb4->put(hdb4, NULL, &key, &data, DB_NOOVERWRITE)) == 0)
                              ;  
                      else
                              return FALSE;
         
                  }
              }

          } // End while

          // End the search to this call
          (void) FindClose( FindHandle );
          return TRUE;

      }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.