-5
    wchar_t* Pfad;
    wcin >> Pfad;

wchar_t* file = Pfad + "*.quiz";

Not working for me, how can I make this work? It says "*.quiz" is wrong, something like it has to be a numeric value or something like that.

Well sorry, I am new to c++...

and my actual problem is, I use this, but I want to user to select the path

something like cin >> file

but I don't want the user to have to type "*.quiz";

wchar_t* file = L"C:/Users/Niku/Documents/Visual Studio 2012/Projects/Quiz/Quiz/Fragen_Niko/*.quiz"; 
WIN32_FIND_DATA FindFileData;
 HANDLE hFind;
hFind = FindFirstFile(file, &FindFileData); 

if( hFind != INVALID_HANDLE_VALUE ) {
max++;
while ((x = FindNextFile(hFind, &FindFileData)) == TRUE)
max++;
}

If I use anything else besides wchar_t* it's not working..

user3566608
  • 353
  • 3
  • 15
  • 11
    You are not ready, use `std::wstring` instead. – Captain Obvlious May 20 '14 at 20:54
  • Also, wide-character string literals are prefixed with an L, like this: `L"*.quiz"` – Rook May 20 '14 at 20:55
  • 5
    [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1171191) – BoBTFish May 20 '14 at 20:56
  • 3
    This is _obviously_ not a duplicate of a list of books. Stupid people. – Lightness Races in Orbit May 20 '14 at 21:00
  • 1
    Short answer: Adding two pointers (`Pfad` and `"*.quiz"`) isn't going to produce anything useful. You need to use a concatenation function to concatenate the string referenced by those pointers, or you need a class that overrides `+` to provide concatenation (aformentioned `wstring`). – ikegami May 20 '14 at 21:03

1 Answers1

4

It's not working the way you are expecting because you don't fully understand pointers and string literals in C++. If you want to know what's wrong, please pick any of these books (possibly beginner ones).

If you want to fix this problem straight ahead (take a shortcut) use an std::wstring instead:

std::wstring Pfad;
std::wcin >> Pfad;
auto file = Pfad + L"*.quiz";

Live demo

As suggested by Rook, if you have to pass a pointer to C APIs you can use the member function std::wstring::c_str, which will return a const wchar_t* for you to use.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • You should probably mention `.c_str()`, as well. It'll be needed to feed to those winapi functions in the OP. – Rook May 20 '14 at 21:05
  • Well thank you, but the problem is, if I use wstring in the line "hFind = FindFirstFile(file, &FindFileData);" I get an error "file - ERROR: std::wstring to LPCWSTR" – user3566608 May 20 '14 at 21:12
  • @user3566608, try with `hFind = FindFirstFile(file.c_str(), &FindFileData);`. – Shoe May 20 '14 at 21:14
  • Thank you! The errors are gone, but if I cin my path "C:/Users/Niku/Documents/Visual Studio 2012/Projects/Quiz/Quiz/Fragen_Niko/" it doesn't work. It says "there are 0 files" with the my old code it works. Maybe it's because of the blanks (/Visual Studio 2012/) ? Sorry for asking, but do you know how I could make this work? However, thank you, you helped me a lot! – user3566608 May 20 '14 at 21:35