Hello, I am trying to open the windows file explorer, choose a file, convert that file, then save that file using the windows file explorer again.
I have some code but i came across a problem with this line.
GetSaveFileNameA(out);
this is the error I get: no suitable conversion function from "std::ofstream" to "LPOPENFILENAMEA" exists
I believe i have to convert the out variable but im not sure how to convert it or what to convert it too.
the rest of the code i have is below. I would appreciate any help with this error.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
int main()
{
char filename[MAX_PATH];
OPENFILENAME ofn;
ZeroMemory(&filename, sizeof(filename));
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL; // If you have a window to center over, put its HANDLE here
ofn.lpstrFilter = "Text Files\0*.txt\0Any File\0*.*\0";
ofn.lpstrFile = filename;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = "Select a File, yo!";
ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
if (GetOpenFileNameA(&ofn))//opens file explorer to pick a file
{
std::cout << "You chose the file \"" << filename << "\"\n";
constchar comma = ','; //converting the file starts at this point
std::string line, word;
std::ifstream in(filename);
std::ofstream out("file.csv");
while (getline(in, line)) {
std::stringstream ss(line);
bool first = true;
while (ss >> word) {
if (!first)out << comma;
out << word;
first = false;
}
out << '\n';
}
in.close();
GetSaveFileNameA(out); //error on this line //this line is meant to open the file explorer to save the converted file
out.close();
}
else
{
// All this stuff below is to tell you exactly how you messed up above.
// Once you've got that fixed, you can often (not always!) reduce it to a 'user cancelled' assumption.
switch (CommDlgExtendedError())
{
case CDERR_DIALOGFAILURE: std::cout << "CDERR_DIALOGFAILURE\n"; break;
case CDERR_FINDRESFAILURE: std::cout << "CDERR_FINDRESFAILURE\n"; break;
case CDERR_INITIALIZATION: std::cout << "CDERR_INITIALIZATION\n"; break;
case CDERR_LOADRESFAILURE: std::cout << "CDERR_LOADRESFAILURE\n"; break;
case CDERR_LOADSTRFAILURE: std::cout << "CDERR_LOADSTRFAILURE\n"; break;
case CDERR_LOCKRESFAILURE: std::cout << "CDERR_LOCKRESFAILURE\n"; break;
case CDERR_MEMALLOCFAILURE: std::cout << "CDERR_MEMALLOCFAILURE\n"; break;
case CDERR_MEMLOCKFAILURE: std::cout << "CDERR_MEMLOCKFAILURE\n"; break;
case CDERR_NOHINSTANCE: std::cout << "CDERR_NOHINSTANCE\n"; break;
case CDERR_NOHOOK: std::cout << "CDERR_NOHOOK\n"; break;
case CDERR_NOTEMPLATE: std::cout << "CDERR_NOTEMPLATE\n"; break;
case CDERR_STRUCTSIZE: std::cout << "CDERR_STRUCTSIZE\n"; break;
case FNERR_BUFFERTOOSMALL: std::cout << "FNERR_BUFFERTOOSMALL\n"; break;
case FNERR_INVALIDFILENAME: std::cout << "FNERR_INVALIDFILENAME\n"; break;
case FNERR_SUBCLASSFAILURE: std::cout << "FNERR_SUBCLASSFAILURE\n"; break;
default: std::cout << "You cancelled.\n";
}
}
}
When I try GetOpenFIleName it doesnt open the file explorer at all. When I do GetOpenFileNameA is opens the file explorer. I am having trouble with GetSaveFileNameA.
A C++ stream object is not convertable to an OPENFILENAME struct, as the error states.
You created an OPENFILENAME struct to open the file for reading, you will likely need to create another to open the file for writing if you want to use GetSaveFileNameA.
You have already read and written the data using C++ streams, to the location of your chosen input file, before you use File Explorer to choose the output file's location.