0

I am using Windows 10 (Visual Studio 2015).

Alright I just want to start off by saying is that I am using an edit box for the user to write a directory,appname,comboboxname then having wofstream output the directory,appname,comboboxname into a .txt file.

My problem is that some directories have spaces so when wifstream reads those spaces then cuts off the directory to early and stores the cut off version into one of the wchar_t variables.

What I need for the program to do (I am not sure if its possible) is output line by line.

Example:

C:\Program Files (x86)\Minecraft

MinecraftLauncher

Minecraft

Then use getline to read the whole line including the spaces, store that line into a variable and then get the next line. (But it also can't grab any extra white spaces that aren't in the directory otherwise it won't work) you guys probably know how directories work lol)

So you guys don't have to look that hard, I want this to occur in the case IDB_CLICK_ME.

Here is my code currently, something I am tinkering with before I decide to add it to my main application:

This is where I am trying to read the file (without whitespaces)

case WM_CREATE:
    {


        wchar_t testData[20] = L"Hai";
        CreateWindow(L"button", L"CLICK ME", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 5, 5, 200, 25, hwnd, (HMENU)IDB_CLICK_ME, NULL, NULL);
        comboBox = CreateWindow(L"combobox", L" ", WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST, 5, 50, 100, 100, hwnd, (HMENU)4, NULL, NULL);
        hProgramName = CreateWindow(L"edit", L"Program Name", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 5, 100, 200, 25, hwnd, NULL, NULL, NULL);
        hProgramDirectory = CreateWindow(L"edit", L"Program Directory", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 5, 130, 200, 25, hwnd, NULL, NULL, NULL);
        hProgramNameComboBox = CreateWindow(L"edit", L"Name listed in ComboBox", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 5, 160, 200, 25, hwnd, NULL, NULL, NULL);
        std::wifstream myfile;
        myfile.open("LaunchLocations.txt");
        myfile >> std::noskipws >> gameLaunchtest.directory;
        myfile >> std::noskipws >> gameLaunchtest.AppName;
        myfile >> std::noskipws >> gameLaunchtest.ComboBoxName;

This is where I am writing it, so far this works correctly

    ComboBox_AddString(comboBox, gameLaunchtest.directory);
    break;


        case WM_COMMAND:
            switch (LOWORD(wparam))
            {
            case IDB_CLICK_ME:
                    GetWindowText(hProgramDirectory, gameLaunchtest.directory, MAX_PATH);
            GetWindowText(hProgramName, gameLaunchtest.AppName, MAX_PATH);
            GetWindowText(hProgramNameComboBox, gameLaunchtest.ComboBoxName, MAX_PATH);
            wofstream launchLocations;
            launchLocations.open("LaunchLocations.txt");
            launchLocations << gameLaunchtest.directory << endl;
            launchLocations << gameLaunchtest.AppName << endl;
            launchLocations << gameLaunchtest.ComboBoxName << endl;
            launchLocations.close();
  • Minor point: `MAX_PATH` is probably a better choice for the maximum length for the path. And as of the question, is it just how to write a line to an output stream? That happens in the same way as in a "hello world" program: `cout << "Hello world\n"`. – roeland Oct 15 '15 at 01:06
  • @ roeland More of really how to read it with the white spaces (until the last character) because if extra white spaces are added onto the end of a directory you probably could guess how much fail that would be. – Trevin Corkery Oct 15 '15 at 01:10
  • `std::getline` reads a line. The main problem is to set up the streams to use UTF-8 for the external data. You need to use some Visual C++ library extensions for that, like `_setmode` IIRC (they're also supported by MingW g++). – Cheers and hth. - Alf Oct 15 '15 at 01:12
  • You should then try to reduce your code to the part where you actually have a problem, i.e. the few lines in the `IDB_CLICK_ME` case. → see [MCVE](http://stackoverflow.com/help/mcve). And include the code where you're trying to read that file back. – roeland Oct 15 '15 at 01:14
  • @roeland I shortened it up a bit, I hope that helps :) – Trevin Corkery Oct 15 '15 at 01:26
  • Well you mentioned the solution right there in your question—use `std::getline()` for reading the strings back. For writing UTF-8 you can look at http://stackoverflow.com/questions/9859020/windows-unicode-c-stream-output-failure/9869272#9869272 – roeland Oct 15 '15 at 02:16
  • The problem with that is I get this error: no instance of overloaded function "std::getline" matches the argument list. Same thing with the getline from #include – Trevin Corkery Oct 15 '15 at 02:19
  • @roeland: `MAX_PATH` is the limit for the ANSI file APIs. The Unicode file APIs allow for 32k-1 Unicode characters, as outlined in the documentation for [CreateFile](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx). – IInspectable Oct 15 '15 at 09:16

1 Answers1

0

So the way I fixed my problem (I was told by some very helper member that I must use the wifstream getline instead of the string getline)

    std::wifstream myfile;
    while (myfile.getline(test[number].directory, 100))
        {
            myfile.getline(test[number].AppName, 100);
            myfile.getline(test[number].ComboBoxName, 100);
            test[number].ID;
            ComboBox_AddString(comboBox, test[number].ComboBoxName);
            number++;
            test[number].ID = test[number].ID + 1;