Link to home
Start Free TrialLog in
Avatar of rsalunkhe
rsalunkhe

asked on

Program to read filenames in a directory

Hi,

I am trying to write a code (using the vc++ compiler) to read the filenames in any given directory in the windows system and then write the list of filenames in a text file.

Example-
Suppose i have a directory "c:\temp" with the files a.txt,b.dll,c.jpg etc.

i give the program the directory to read as "c:\temp" , then i want my code to read the file names in this directory and then output a file list.txt containing the names of the files ( that is a.txt, b.dll, c.jpg.....)

I guess it will be a small piece of code but i do not know how to do it on windows.

[ i know it can be done on unix using opendir(),readdir() functions but these do not seem to be available in vc++ and do not know the equivalents]

Thanks

ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that i found from some Previously asked questions, and it was acceptable to questioner , i hope this works for u also
Avatar of DarthNemesis
DarthNemesis

The opendir(), readdir() etc. functions are available in c++ if you include the dirent.h library.

#include <stdio.h>
#include <dirent.h>
#include <vector.h>
#include <string.h>
#include <fstream.h>

int main() {
       DIR *dir_p;
       struct dirent *dir_entry_p;
       dir_p = opendir("c:\\temp\\");
       vector<string> filelist(0);
       while (NULL != (dir_entry_p = readdir(dir_p)))
       {
               if (strcmp(dir_entry_p->d_name,".") && strcmp(dir_entry_p->d_name,".."))
                       filelist.push_back(dir_entry_p->d_name);
       }
       ofstream outfile("list.txt");
       for (int i=0; i<filelist.size(); i++) {
               outfile << filelist[i] << endl;
       }
       outfile.close();
       return 0;
}
Avatar of rsalunkhe

ASKER

Hi,

DarthNemesis , i do not have dirent.h but it does seem ur code will do the trick. Thanks for your feedback.

However akshayxx i used ur code and it worked for me. Thanks for your feedback too.

Just one question.... below is the code i am using with the extra lines of writing to a file. I was just wondering how can i give another location for the detination file list.txt?

If i use     fopen("list.txt","w")     it creates it in the location of the code directory. How do i write the output file to a location C:\Backup ?

fopen("C:\backup\list.txt","w") does NOT do the trick!

-------------------
#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
#include<windows.h>

main()
{

BOOL  Done;                    // Done searching for files?
 HANDLE  FndHnd = NULL;   // Handle to find data.
 WIN32_FIND_DATA FindDat;  // Info on file found.
 
FILE *f;

 if ((f=fopen("list.txt","w")) == NULL)
    {
        cout<<"Unable to open the file";
        return 0;
    }

 f=fopen("c:\list.txt","w");

 FndHnd = FindFirstFile("E:\\*", &FindDat);
 Done = (FndHnd == NULL);       // Indicate we are done if nothing was found.

 while (!Done)
 {
    cout <<  FindDat.cFileName << endl; // Print name of file found.
     fputs(FindDat.cFileName,f);
     fputs("\n",f);
    Done = !FindNextFile(FndHnd, &FindDat);
 }
 if (FndHnd)        // If there was anything found, then
    FindClose(FndHnd); // Close the find handle

  fclose(f);
     
}

---------------

Thanks,
Ranjit
> fopen("C:\backup\list.txt","w") does NOT do the trick!
\ is the escape character, and it has special meaning to a text string. If you want to use a literal \ character, you need to double up on the backslashes like so: fopen("C:\\backup\\list.txt","w").
DarthNemesis already answered the '\\' problem .. that will work ..
Hi,

DarthNemesis , i do not have dirent.h but it does seem ur code will do the trick. Thanks for your feedback.

However akshayxx i used ur code and it worked for me. Thanks for your feedback too.

Just one question.... below is the code i am using with the extra lines of writing to a file. I was just wondering how can i give another location for the detination file list.txt?

If i use     fopen("list.txt","w")     it creates it in the location of the code directory. How do i write the output file to a location C:\Backup ?

fopen("C:\backup\list.txt","w") does NOT do the trick!

-------------------
#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
#include<windows.h>

main()
{

BOOL  Done;                    // Done searching for files?
 HANDLE  FndHnd = NULL;   // Handle to find data.
 WIN32_FIND_DATA FindDat;  // Info on file found.
 
FILE *f;

 if ((f=fopen("list.txt","w")) == NULL)
    {
        cout<<"Unable to open the file";
        return 0;
    }

 f=fopen("c:\list.txt","w");

 FndHnd = FindFirstFile("E:\\*", &FindDat);
 Done = (FndHnd == NULL);       // Indicate we are done if nothing was found.

 while (!Done)
 {
    cout <<  FindDat.cFileName << endl; // Print name of file found.
     fputs(FindDat.cFileName,f);
     fputs("\n",f);
    Done = !FindNextFile(FndHnd, &FindDat);
 }
 if (FndHnd)        // If there was anything found, then
    FindClose(FndHnd); // Close the find handle

  fclose(f);
     
}

---------------

Thanks,
Ranjit
thanks guys i appreciate the help.