View Single Post
  #1 (permalink)  
Old 02-12-2008, 05:49 AM
shariath shariath is offline
Newbie
 
Join Date: Feb 2008
Posts: 1
Rep Power: 0
shariath is on a distinguished road
Default OPENFILENAME struct (win32) - problems

Hello,

I'm having some problems relating this sctructure that create Common Dialog Box (open/save) file. In my testing program below, I'm just trying to save the data of a class and after that read them.
I'm using win32, not MFC.

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <windows.h>

using namespace std;

void createfile();
void savefile();
void loadfile();

class myclass {
	int a;
	int b;
	int c;
public:
	myclass(int j, int k, int l) { a=j; b=k; c=l; }; 
	int geta() { return a; };
	int getb() { return b; };
	int getc() { return c; };
};

fstream tmpFile;
char cstrFileName [100];

int main()
{
	cstrFileName[0] = '';

	bool b = false;
	int choice;

	do {
		cout << "1 - Create file\n2 - Load\n3 - Exit\n";
		cin >> choice;
		switch(choice)
		{
			case 1: { createfile(); continue; } break;
			case 2: { loadfile();	continue; } break;
			case 3: { exit(0);		continue; } break;
			default: b = true; system("cls");
		}
	} while(b);
	
	system("pause");
}

void createfile()
{
	myclass a(1, 1, 1);

	OPENFILENAME ofn	= {0};
	ofn.hInstance		= NULL;
	ofn.hwndOwner		= NULL;
	ofn.lStructSize		= sizeof(OPENFILENAME);
	ofn.lpstrFilter		= "CL List Files*.clf*";
	ofn.lpstrTitle		= "Save list";
	ofn.lpstrFile		= cstrFileName;
	ofn.nMaxFile		= sizeof(cstrFileName);
	ofn.Flags			= OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;


	if(GetSaveFileName(&ofn))
	{
		tmpFile.open(cstrFileName, ios::binary);
		tmpFile.write((char *) &a, sizeof(myclass));
		tmpFile.close();	
	}
}

void loadfile()
{
	myclass b(0, 0, 0);

	OPENFILENAME ofn	= {0};
	ofn.hInstance		= NULL;
	ofn.hwndOwner		= NULL;
	ofn.lStructSize		= sizeof(OPENFILENAME);
	ofn.lpstrFilter		= "CL List Files*.clf*";
	ofn.lpstrTitle		= "Open a file";
	ofn.nMaxFile		= sizeof(cstrFileName);
	ofn.Flags			= OFN_FILEMUSTEXIST			|
						OFN_HIDEREADONLY	|
						OFN_NONETWORKBUTTON |
						OFN_PATHMUSTEXIST;

	if(GetOpenFileName(&ofn))
	{
		tmpFile.open(cstrFileName, ios::binary);
		tmpFile.read((char *) &b, sizeof(class myclass));
		
		cout << endl;
		cout << "A = " << b.geta() << endl;
		cout << "B = " << b.getb() << endl;
		cout << "C = " << b.getc() << endl;
		tmpFile.close();
	}
}
The code executes, there's no compilation erros, but the file is not created
Can someone light my path? I tried almost everything and nothing seems to work...

Well, thanks from now on.
Reply With Quote

Sponsored Links