Solved

get file names of directory

Posted on 2013-12-03
11
406 Views
Last Modified: 2013-12-04
Hi,

I am using C with Visual Studio 2010. I need help to get file names of directory. Can anyone please help me how to get file names of directory.

Thanks
0
Comment
Question by:Johny12345
[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
  • 4
  • 4
  • 3
11 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39692797
This is what you wanted:
http://msdn.microsoft.com/en-us/library/07wt70x2%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

Directory::GetFiles(path) Method - retrieves the files in a directory supplied to the method.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 39692798
Hi Johny12345,

you can use FindFirstFile and FindNextFile from Win API like i.e. demonstrated here: http://cboard.cprogramming.com/windows-programming/83267-msdn-example-code-findfirstfile-findnextfile.html

Hope that helps,

ZOPPO
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39692804
ps.  It has an example program there demonstrating the usage
0
Technology Partners: 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!

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39692813
Note - my comment uses C++.net (one of your tags), the comment from Zoppo is using classical C (not .NET)
0
 

Author Comment

by:Johny12345
ID: 39692877
I have used code like this but it is not taking directory properly, can anyone help me please

void getFiles(char *address)
{
 printf(address );  // this line is showing strange characters
 WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   DWORD dwError;
   LPSTR DirSpec;
   size_t length_of_arg;
 
 
   DirSpec = (LPSTR) malloc (BUFSIZE);
    
   // Check for command-line parameter; otherwise, print usage.
   if(argc != 2)
   {
      printf("Usage: Test <dir>\n");
      return 2;
   }
 
   // Check that the input is not larger than allowed.
   StringCbLength(argv[1], BUFSIZE, &length_of_arg);
   if (length_of_arg > (BUFSIZE - 2))
   {
      printf("Input directory is too large.\n");
      return 3;
   }

.
.
.
.
.
.
.

}

Open in new window

0
 
LVL 31

Expert Comment

by:Zoppo
ID: 39692901
Could you post the code where you call the function?
0
 

Author Comment

by:Johny12345
ID: 39692922
it is bit difficult to post all code because it too big but I am posting related code here:

int main(int argc, char *argv[])
{

char* directory_path;

directory_path = (char*) malloc(strlen(argv[2])+4);
getFiles(TEXT(directory_path));

}

Open in new window

0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 39692940
Well, the directory_path is allocated but the data it points to is uninitialized.

IMO you don't need to allocate a string at all, you can simply call something like:

if ( argc > 2 )
{
 getFiles( argv[2] );
}

ZOPPO
0
 

Author Comment

by:Johny12345
ID: 39693325
thanks it worked. Is it possible to put names in dynamic array? Do I need to open another question for it?
0
 

Author Closing Comment

by:Johny12345
ID: 39693328
great.. thanks
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 39694831
Hi,

you're welcome, I'm glad I could help.

About dynamic array: I guess you want to be able to return the list of found filenames to the calling function, right?

Using a C-Array in this case (usually with pointer to pointer, i.e. char**) is IMO not the best solution since you don't know the number of entries, i.o.w. you don't know how many char* you need to allocate which means you'll have to realloc the buffer repeatedly.

Maybe a better idea would be to implement some simple linked list, if needed it's easy to move it into an array afterwards with only one allocation.

Do you know how to implement a simple single linked list in C?

BTW: Just to go sure I would like to ask if you are really using plain C or if you even could use C++, many things are much easier with C++ and STL, with other libraries like i.e. BOOST you can do what you need really simpel (beside the fact if it's new for you you'll have to invest quite some time to get familiar with it).

ZOPPO
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2015 error "not a valid Win32 application" 6 876
C Programming - If Statement 8 84
delete-remove 14 117
Excel Macro update each line based on another worksheet 2 59
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

759 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