Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-12-2008, 04:49 AM
shariath shariath is offline
Newbie
 
Join Date: Feb 2008
Posts: 1
Credits: 0
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 02-14-2008, 10:07 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 606
Last Blog:
Programs Under the Hoo...
Credits: 193
Rep Power: 11
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

Try using (ios_base::binary|ios_base::out) instead of just ios::binary when opening the file for writing.

Last edited by dargueta; 02-14-2008 at 10:08 PM. Reason: Removed accidental smilies
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct notation kenna C and C++ 1 12-23-2007 12:55 PM
[C]Functions Help! mtber C and C++ 1 12-05-2007 08:25 AM
sizeof Struct Eagle_ C and C++ 2 11-18-2007 10:15 AM


All times are GMT -5. The time now is 02:03 PM.

Contest Stats

Xav ........ 1315.71
MeTh0Dz|Reb0rn ........ 1053.7
morefood2001 ........ 879.43
John ........ 877.37
marwex89 ........ 869.98
WingedPanther ........ 814.88
Brandon W ........ 710.1
chili5 ........ 300.72
Steve.L ........ 225.26
dargueta ........ 192.86

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads