?
Solved

Scanning a Directory in Visual C++

Posted on 2003-02-20
6
Medium Priority
?
3,831 Views
Last Modified: 2013-12-14
I need to write a program that compiles several files into one big file so I can do work on that big file. I'm fairly new to windows programming and am having a hard time figuring out how to scan a directory. The directory I need to scan will contain a type of file with a .dat extension and nothing else. I need to find the first file, place it in the bigger file, and then delete it. I can handle putting it in the bigger file and deleting it but I have not had any success scanning the directory and selecting files. Any help would be really appreciated!
0
Comment
Question by:tkrokowski
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 7990657
Standard C++ provides no mechanism for searching directories or for deleting files.  It must be done using an OS-specific technique.  So we need to know what OS you are using.

Here's an example (cut from an earlier EE post) for WINDOWS:

#include <stdio.h>
#include <io.h>
#include <time.h>

void main( void )
{
    struct _finddata_t c_file;
    long hFile;

    /* Find first .c file in current directory */
    if( (hFile = _findfirst( "*.c", &c_file )) == -1L )
       printf( "No *.c files in current directory!\n" );
   else
   {

   /* do something with file */
            /* Find the rest of the .c files */
            while( _findnext( hFile, &c_file ) == 0 )
            {
  /* do something with remaining files */
            }

       _findclose( hFile );
   }
}
0
 

Accepted Solution

by:
Fallen_Knight earned 200 total points
ID: 7990676
basicly useing win32 API:

fill out a
LPWIN32_FIND_DATA struct

call FindFirstFile()

call FindNextFile() repeatedly



heres a baisc example, (memory is not allocated, you'd need to change that) it returns a pointer to a list of files and subdirs in the directory

char **ListDirContents(char *path) {
     int i=0, files;
     char list[100][255];
     WIN32_FIND_DATA find_data;
     HANDLE hnd;

     find_data.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
     if((hnd = FindFirstFile(path, &find_data)) == INVALID_HANDLE_VALUE) {
          return(NULL); //no flies or error
     }
     
     do {
          list[i++][0] = strdup(find_data.cFileName);
          list[i][0]   = NULL;
     } while(FindNextFile(hnd, &find_data));
     
     FindClose(hnd);
     return(list);
}

For more information use MSDN or google to look up those win32 functions.

hope that helps
0
 
LVL 6

Expert Comment

by:gj62
ID: 7990680
Slight modification - ANSI C provides for remove() to delete a file, and,  of course, can be called in C++
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tkrokowski
ID: 7993903
Let me clarify my original question further by saying that this program is being developed in Microsoft Visual C++ 6.0.
0
 

Author Comment

by:tkrokowski
ID: 7993963
Let me clarify my original question further by saying that this program is being developed in Microsoft Visual C++ 6.0.
0
 

Author Comment

by:tkrokowski
ID: 7995805
That was the general premise of what I needed to do. Good job! I tweaked the code a little...



#include <iostream>
#include <windows.h>

#include <vector>
#include <string>
using namespace std;



vector<string> ListDirContents(char *path) {
    int i=0, files;
   vector<string> list;
    WIN32_FIND_DATA find_data;
    HANDLE hnd;

    find_data.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
    if((hnd = FindFirstFile(path, &find_data)) == INVALID_HANDLE_VALUE)
         return list; //no flies or error
    do
     {
         list.push_back( find_data.cFileName );
     }
    while(FindNextFile(hnd, &find_data));
   
    FindClose(hnd);
    return list;
}

int main()
{
     vector<string> test = ListDirContents("C:\\test\\*.*");

     vector<string>::iterator itr = test.begin();

     cout << test.size() << endl;

     while( itr != test.end() )
     {
          cout << *itr << endl;
          itr++;
     }

     return 0;
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question