Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Scanning a Directory in Visual C++

Posted on 2003-02-20
6
Medium Priority
?
3,835 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
  • 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

571 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