Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8693957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:40:01+00:00 2026-06-13T00:40:01+00:00

I try to write function that return array of string(all files and folders that

  • 0

I try to write function that return array of string(all files and folders that located in some directory). Code is below. When I use this code for output info to console (using comented code below) it work great, but when I am try return pointer to pointer to wchar_t I obtain array where every element is equal to enother(all elements are the same). What I do wrong?

wchar_t path[SIZE];
    wchar_t *PathCreator(wchar_t *dir, wchar_t *fileName)
    {
        int j = 0;
        while(j < SIZE)
        {
            path[j] = '\0';
            j++;
        }
        int i;
        i = 0;
        while(*dir != '*' && *dir != '\0')
        {
            path[i] = *dir;
            i++;
            dir++;
        }
        wchar_t *t = fileName;  
        while(*t != '\0')
        {
            path[i] = *t;
            i++;
            t++;
        }
        path[i] = '\0';
        return path;
    }
wchar_t* allFlsArr[SIZE];
int i = 0;
wchar_t **GetAllFiles(wchar_t* dir)
{
    WIN32_FIND_DATA file;
    HANDLE search_hendle = FindFirstFile(dir, &file);
    if(search_hendle)
    {
        do
        {
            wchar_t *p = PathCreator(dir,file.cFileName);
            //std::wcout << p << std::endl;
            allFlsArr[i++] = p;
            std::wcout << i << std::endl;
        }
        while(FindNextFile(search_hendle, &file));
        allFlsArr[i] = '\0';
    }
    CloseHandle(search_hendle);
    return allFlsArr;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:40:02+00:00Added an answer on June 13, 2026 at 12:40 am

    I would suspect you are not allocating the memory for the return from PathCreator. This means you set the same pointer at every entry. When you change the data at the pointer location it gets changed and as every entry is looknig at that data everything gets “changed”.

    You would be far better off using std::wstring …

    std::wstring PathCreator(const std::wstring& dir, wchar_t *fileName)
    {
        return dir.substr( 0, dir.size() - 1 ) + fileName;
    } 
    
    std::vector< std::wstring > GetAllFiles( const std::wstring& dir)
    {
        std::vector< std::wstring > allFlsArr;
        WIN32_FIND_DATA file;
        HANDLE search_handle = FindFirstFile(dir.c_str(), &file);
        if(search_handle)
        {
            do
            {
                std::wstring path = PathCreator(dir,file.cFileName);
                //std::wcout << path << std::endl;
                allFlsArr.push_back( path );
                std::wcout << i << std::endl;
            }
            while(FindNextFile(search_handle, &file));
        }
        CloseHandle(search_handle);
        return allFlsArr;
    }
    

    You would need to change the corresponding code in PathCreator to use std::wstring as well.

    Edit: If you really won’t use STL then you need to modify PathCreator to do something like the following:

    wchar_t *PathCreator(wchar_t *dir, wchar_t *fileName)
    {
        wchar_t* path = new wchar_t[SIZE];
        int j = 0;
        while(j < SIZE)
        {
            path[j] = '\0';
            j++;
        }
        int i;
        i = 0;
        while(*dir != '*' && *dir != '\0')
        {
            path[i] = *dir;
            i++;
            dir++;
        }
        wchar_t *t = fileName;  
        while(*t != '\0')
        {
            path[i] = *t;
            i++;
            t++;
        }
        path[i] = '\0';
        return path;
    }  
    

    Don’t forget to delete[] the strings when you’ve finished with them!

    You really are, however, creating pain for yourself by not using the STL functions and the RAII bonuses that they provide. My method would require no remembering to delete the memory used afterwards as when it drops out of scope that memory will automatically be freed.

    I really thing the first method is waaaay easier though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a delegate function that can 'wrap' some while/try/catch code around
I've been trying to write some very fast Java code that has to do
This is the code that I want to try to write: #include <stdio.h> #include
I write simple C++ code that compute array reduction sum, but with OpenMP reduction
I'm trying to write a recursive function that return a List < int *
I am trying to write a JavaScript function that will return its first argument(function)
I need a simple function that writes an array of files to one zip
I try to write a javascript Self-Executing Anonymous Function window.App = window.App || {}
try to write a composite component that allows mutltiple text inputs. I read that
I try to write Activator action for changing current song rating. I now that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.