Listing files in Directory

Hi guys,

new comer to C++ here.
I am interested in a simple program for klm-files manipulation.
Having some C-practice in files manipulation shouldn't be a big deal.

Where I am stuck is reading the directory.
So far could not find way to do it,
and tried some example code chains proposed here.
Unfortunately errors are displayed, upper to my skill level.

Last one I am experimenting with is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <Windows.h>
#include <vector>

 vector<string> get_all_files_names_within_folder(string folder)
{
	vector<string> names;
	char search_path[200];
	sprintf_s(search_path, "%s*.*", folder.c_str());
	WIN32_FIND_DATA fd;
	HANDLE hFind = ::FindFirstFile(search_path, &fd);
	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			// read all (real) files in current folder
			// , delete '!' read other 2 default folder . and ..
			if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
				names.push_back(fd.cFileName);
			}
		} while (::FindNextFile(hFind, &fd));
		::FindClose(hFind);
	}
	return names;
}

std::vector<string> names = get_all_files_names_within_folder(Source_KML_files_DIR);


This one compiled w/o errors, but I can't access "name's" members,
could someone provide working advise for MS Visual Studio 2013?

Regards,
K.
I think that might be moaning about line 14, as you've missed the std::. (and you've missed it on line 12 as well for both vector and string).
Where is line 32? Is that in another function or in main or something?
Last edited on
Thnks, mutexe,

line 32 is in "main's" body, compiles and links OK.

Re std:: I've put "using namespace std;" on top,
apologies for not copy/pasting it also here.

Access is a problem so far,
tried this lately, also compiles but w/o printout:

for (auto &i : names)
std::cout << i << endl; //output one element per line

Regards, K.
Last edited on
Problem solved.

Above,
....
for (auto &i : names)
std::cout << i << endl; //output one element per line
...

printed nothing due to my mistake/ wrong paths :)

I tried this one
....
for(std::vector<string>::iterator it = text.begin(); it != text.end(); ++it)
std::cout << *it << endl;
.....

from here http://www.cplusplus.com/forum/beginner/104130/
also working ok,

many thanks to Thumper also!

Regards,
K.

Topic archived. No new replies allowed.