Help with GetOpenFileName and GetSaveFileName

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.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#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";

		const char 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";
		}
	}
}
  
Last edited on
Try using "GetOpenFileName" not "GetOpenFileNameA".
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.
closed account (E0p9LyTq)
tipaye wrote:
Try using "GetOpenFileName" not "GetOpenFileNameA".

He has to specify the ANSI version of the function, he is using a char array file name.
closed account (E0p9LyTq)
@bkara,

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.
Last edited on
Thanks for the reply FurryGuy,

What would be the best way to go about doing that? could you share an example or share some code?
closed account (E0p9LyTq)
You've already done the work, coding how to use File Explorer to choose the name and location of the file you want to open for reading.

Look at how your code is structured for using File Explorer allowing you to choose the file to open.

You've retrieved the filename/file-path for the input file, opened it as a file stream, read from the file, and closed the file stream.

Duplicate that code for using File Explorer to choose the name and location of your save file.

Before you open the file to save your data as an output file stream.
Topic archived. No new replies allowed.