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 8531593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:31:19+00:00 2026-06-11T09:31:19+00:00

I ahve the following piece of code. I get a correctly filled vector. But

  • 0

I ahve the following piece of code. I get a correctly filled vector. But I am unable to print or use the vector contents which are file names from a directory. As soon as I do enter the first iteration. Everything gets lost. What am I doing wrong?

wprintf - This works OK

wcout-- here is where everything ends up corrupted

#include <windows.h>
#include <sstream>
#include <string>
#include <vector>
#include<iostream>
void GetAllFiles(vector<LPCWSTR>&, wstring);
using namespace std;
void main (void)
{
    vector<LPCWSTR> files(0);
    wstring path = L"Datasets\\Persons\\";
    wstring ext = L"*.*";
    wstring fullPath = path+ext;
    GetAllFiles(files,fullPath);    
    for (unsigned i=0; i<files.size() ; i++)
    {
        try
        {
            wcout<<"::\n"<<files[i];
        }
        catch(exception &ex)
        {
            cout<<"Error:"<<ex.what();
        }
    }

}

void GetAllFiles(vector<LPCWSTR>& fileNames,wstring dir)
{

    WIN32_FIND_DATA search_data;
    memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
    HANDLE handle = FindFirstFile(dir.c_str(),&search_data);
    while(handle != INVALID_HANDLE_VALUE)
    {
        wprintf(L"Found file: %s\r\n", search_data.cFileName);
        fileNames.push_back(search_data.cFileName);
        if(FindNextFile(handle, &search_data) == FALSE)
            break;
    }   
}

I have attached a screen shots of the output.

Correct when read off disk in GetAllFiles(...)

Corrupted as soon as the loops first iteration is done

  • 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-11T09:31:20+00:00Added an answer on June 11, 2026 at 9:31 am

    search_data.cFileName is a pointer to memory controlled by the FindFirstFile/FindNextFile iterator interface; you cannot store this pointer value as the pointed-to memory could change from iteration to iteration (or even be freed after the iteration completes).

    Instead, you must make a copy of the string to put in your vector, e.g. using wcsdup. Even better, define your vector as a vector<wstring>, so that push_back(search_data.cFileName); creates a wstring with the contents of search_data.cFileName.

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

Sidebar

Related Questions

I ahve a html file, in which I include .js file. This file contains
I ahve a jquery code that get the siblings of a table td text.
I am filtering the all list which ahve same lat,long in one list and
i am making a project in which i ahve shedule somthing happen after a
Currently I am using a DLL Library to get lists of custom objects. Which
I currently have the following code that retrieves data from the database and then
I have a xml code that can ahve two forms : Form 1 <?xml
I have some c# code running on sharepoint that i use to check inside
I am sure this should be easy but I ahve been going crazy trying
i ahve a class named FiniteStateMachine declared as below header file : FiniteStateMachine.h class

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.