|
See, the best way (that i feel) of reading and writing srtuctured data to a file is using the following functions:
istream::read
ostream::write
Function prototypes are: read( const unsigned char* ptr, int n)
Description: The read function reads n characters starting form the location pointed to by ptr. In other words, read function reads n bytes from the address pointed to by ptr.
The write has similar prototype and similar description (but used to write data)
The above functions work best for reading/writing classes and structures from/to files.
EXAMPLE:I include all your data in a class, say "A"
class A<br />
{<br />
double x;<br />
double y;<br />
double speed;<br />
double z;<br />
int something1;<br />
char something2;<br />
};
Then i write all these this to a file using the code:
main()<br />
{<br />
A objectA;<br />
cin>>objectA.x;<br />
cin>>objectA.y;<br />
cin>>objectA.z;
<br />
ofstream outfile ("c:\\test.txt", ios::binary);<br />
outfile.write ((char*)objectA, sizeof(objectA)); <br />
}
Once the object has been written into a file, you can read it anytime using using the code:
main()<br />
{<br />
A objectB;<br />
ifstream infile ("c:\\test.txt", ios::binary);<br />
infile.read ((char*)objectB, sizeof(objectB)); <br />
cout<<objectA.x;<br />
cout<<objectA.y;<br />
cout<<objectA.z;
}
YOU CAN CALL WRITE AGAIN AND AGAIN TO APPEND MORE TAHN ONE RECORDS INTO THE FILE
SIMILAR IS THE READ FUNCTION. WHEN U CALL READ AGAIN, IT WILL READ THE NEXT OBJECT WRITTEN INTO THE FILE.
All this may appear a bit fuzzy and complicated to you, BUT TRUST ME, this is the best way to handle structured data in files in C++.
Ask if u have any doubts;)
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
|
|
|
|
|
Ok I like this idea. I will try to use it. Could i, in case i want more details contact u on ur private mail?
THX
|
|
|
|
|
If u follow the above mentioned technique, u can display any field, any time. Also u dont have to mess with the seek stuff. Thats very unreliable.
You can personally contact me at avenger_sb25@yahoo.com or at avenger_sb25@hotmail.com
I daily check both these mails.
If u want, u can add me to your Yahoo Messenger or MSN Messenger
...cya
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
|
|
|
|
|
You could if each line were the same length, or each xpos/ypos/speed group was the same size. The seekg() method is used to position the file pointer for the next read operation.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
First thx for friday.
Then what do u mean by the same length? Is space or tab considered as getting in the value's lenth? Or just by adding zeros at the end?
I looked in a tutorial for the skeeg stuff and didn't understand well how it works. Could u breif me about this, please?
THX
|
|
|
|
|
dairiseky wrote:
Is space or tab considered as getting in the value's lenth?
Yes, just because the character is not printable does not make it any less of a character.
The seekg() method goes to a certain position within the file. It knows nothing of line-length. So if you had a two-line file, with the first line being 5 characters and the second line being 15 characters, seekg(6) would go to the start of the second line, but if the length of the first line ever changed, it wouldn't. Understand?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
OK thx. Got the trick ! sorry for the deformation of skeeg I was in a rush to get the train.
|
|
|
|
|
OK thx. Got the trick ! sorry for the deformation of skeeg I was in a rush to get the train.
|
|
|
|
|
Is it possible to get the size of datas to be written to file and then use this (their size), to be able to retrieve the right data?
THX
|
|
|
|
|
Yes. Wasn't that answered here?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Dear Friends,
I want to perform certain operation when the dialog "just appears" on screen. I dont want to perform this operation in OnInitDialog(), because at that time dialog is not appeared on the screen.
Which message is get posted when it is ensured that dialog is just diaplayed on the screen ?? Or is there any other way ?
Imtiaz
|
|
|
|
|
Well, I can think of one possible solution:
Whenever the windows is dispayed (for the first time too) it definitely receives a WM_PAINT message.
You can use the following trick:
<br />
static int i=0;
OnWmPaint()<br />
{<br />
if(i==0)<br />
{<br />
_____(LINE1)<br />
i++;<br />
}<br />
else<br />
{<br />
}<br />
}
Now, if you want to execute LINE1 whenever window is displayed, reset the value of 'i' to 0 when you hide or close the dialog box.
NOTE:Don't worry, you can intercept WM_PAINT message even for a dialog boxI hope this trick will solve your purpose.
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
|
|
|
|
|
Imtiaz Murtaza wrote:
Or is there any other way ?
Use PostMessage() at the end of OnInitDialog() . This allows the function to return so that the message pump gets started, and the dialog gets rendered. At that point, your message handler should get called.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Hi,
Can anyone pls provide me in getting some useful sites for preparing for
C++/VC++ interview.SSites which have some code snippets or examples.
Pls help.
Ram
|
|
|
|
|
And this is going to help how, exactly?
CP is full of examples and wise comments about VC++ and C++ generally, but that won't help in an interview unless the people doing the interview are looking for (or expecting) someone who doesn't have real-world experience.
I'm familiar with the 'how do you get experience if you can't get a job' paradox. Me, I got lucky, but I've heard of cases where people lied outright about their experience and were stupid enough to believe they'd be able to bluff it until they knew enough. In those same cases, they tended not to last long.
And before anyone leaps on their high horse to say how dare I suggest that Ram is going to lie in an interview, I'm not suggesting anything of the kind, it's merely an aside about what happens if you try and beat the 'experience' system.
Steve S
|
|
|
|
|
Steve S wrote:
I've heard of cases where people lied outright about their experience and were stupid enough to believe they'd be able to bluff it until they knew enough. In those same cases, they tended not to last long
Cough Cough....3 years...Cough Cough...
"Now I guess I'll sit back and watch people misinterpret what I just said......"
Christian Graus At The Soapbox
|
|
|
|
|
Well, always remember this thing- In interviews covering C/C++ the main focus is on concepts and techniques not how many programs you have made or how big are the programs u hav made.
Focus on the following topics-
1. Pointers (HOTTEST TOPIC with raw C++ ,esp. "Function Pointers", Pointer to arrays etc.)
2. Threads (Common topic with VC++)
3. Message Queues Architecture (VC++)
4. There r lot of books on "objective/practice questions in C++/VC++". Pick any of them and go through them
Rest is up 2 ur effort;)
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
|
|
|
|
|
Hi,
Wt is the difference between HTTP proxy and SOCKS
plz tell me
|
|
|
|
|
|
Hi,
I'm searching for some starting point about generating a database chart from a given database. I know it's not a trivial problem but I only need to make it work for some simple cases ( 4 til 10 tables, not more).
Do you know where can I find something interesting ? Thanks in advance,Greetings
Braulio
|
|
|
|
|
I'm having some nasty problems with the MFC class CFileDialog. I'm simply trying to use it to get the name of a selected file and put it in a text edit box. I have done this many times before and have never had this sort of problem. Here is the code that is producing the error:
[code]
void CFileSplitterDlg::OnButtonOpenSplitFile()
{
CFileDialog FileOpen(TRUE,0,0,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,0,this);
FileOpen.m_ofn.lpstrTitle="Select File to Split";
int Result=FileOpen.DoModal();
if (Result==IDOK)
{
SetDlgItemText(IDC_EDIT_SPLIT,FileOpen.GetPathName());
m_ProgressSplit.SetPos(0);
}
else
{
if (Result==IDCANCEL)
{
SetDlgItemText(IDC_EDIT_SPLIT,"");
}
}
}
[/code]
IDC_EDIT_SPLIT is defined, and m_ProgressSplit is a valid member variable. I also looked up on the MSDN anything I could find on CFileDialog and access violations and found something regarding custom CFileDialog classes and access violations, the issue is apparently fixed in windows 2000 (I'm running XP Pro), and is also fixed in VS6SP6 (I was running VS6 with no service pack when I encountered this error, I have since upgraded to SP6, this of course did not help), so I'm pretty sure it's not that I'm missing anything. Originally I was using MFC dlls, I tried switching to static MFC libraries to see if that had any effect, it didn't. I also tried created a brand new dialog based MFC application and made it so the only thing it did was create a CFileDialog when the default 'OK' button was pressed. I got the same error (which leads me to believe it is not something isolated to my project).
I've narrowed it down to the destructor of CFileDialog. When I press the 'Open Split File' (which calls CFileSplitterDlg::OnButtonOpenSplitFile()) button the CFileDialog is created and DoModal() is called (causing the dialog to appear). It then lets me select a file. If I press 'Open' it returns IDOK and the file name gets put into the text edit box. If I select 'Cancel' IDCANCEL is returned and anything inside the text edit box is cleared (as it should be). The error doesn't occur untill the destructor is called. Which of course happens when the CFileDialog goes out of scope as soon as the function. Everything I have before then works correctly (I used message boxes to stop determine the point just before it crashes). I even commented out everything but the constructor and still got an error when the destructor is called.
The error message it says when the program crashes when I run it through the debugger says, "Unhandled exception in File Splitter.exe (KERNEL32.DLL): 0xC0000005: Access Violation."
Here is the call stack the debugger gives me.
[code]
KERNEL32! 77e778ce()
CFileDialog::~CFileDialog() + 71 bytes
CFileSplitterDlg::OnButtonOpenSplitFile() line 198 + 18 bytes
_AfxDispatchCmdMsg(CCmdTarget * 0x0012fd74 {CFileSplitterDlg}, unsigned int 1007, int 0, void (void)* 0x00401078 CFileSplitterDlg::OnButtonOpenSplitFile(void), void * 0x00000000, unsigned int 12, AFX_CMDHANDLERINFO * 0x00000000) line 88
CCmdTarget::OnCmdMsg(unsigned int 1007, int 0, void * 0x00000000, AFX_CMDHANDLERINFO * 0x00000000) line 302 + 39 bytes
CDialog::OnCmdMsg(unsigned int 1007, int 0, void * 0x00000000, AFX_CMDHANDLERINFO * 0x00000000) line 97 + 24 bytes
CWnd::OnCommand(unsigned int 1007, long 2818546) line 2099
CWnd::OnWndMsg(unsigned int 273, unsigned int 1007, long 2818546, long * 0x0012f82c) line 1608 + 28 bytes
CWnd::WindowProc(unsigned int 273, unsigned int 1007, long 2818546) line 1596 + 30 bytes
AfxCallWndProc(CWnd * 0x0012fd74 {CFileSplitterDlg hWnd=???}, HWND__ * 0x00a3027a, unsigned int 273, unsigned int 1007, long 2818546) line 215 + 26 bytes
AfxWndProc(HWND__ * 0x00a3027a, unsigned int 273, unsigned int 1007, long 2818546) line 379
USER32! 77d67b17()
USER32! 77d6cdce()
USER32! 77d45696()
USER32! 77d461f6()
USER32! 77d5ec02()
USER32! 77d5c3f6()
USER32! 77d67b17()
USER32! 77d6cdce()
USER32! 77d44435()
USER32! 77d49611()
USER32! 77d5d704()
CWnd::IsDialogMessageA(tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 182
CWnd::PreTranslateInput(tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 3435
CDialog::PreTranslateMessage(tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 92
CWnd::WalkPreTranslateTree(HWND__ * 0x00a3027a, tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 2678 + 18 bytes
CWinThread::PreTranslateMessage(tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 672 + 18 bytes
CWinThread::PumpMessage() line 848 + 30 bytes
CWnd::RunModalLoop(unsigned long 4) line 3489 + 19 bytes
CDialog::DoModal() line 539 + 12 bytes
CFileSplitterApp::InitInstance() line 52 + 11 bytes
AfxWinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x00141f3c, int 1) line 39 + 11 bytes
WinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x00141f3c, int 1) line 30
WinMainCRTStartup() line 198 + 54 bytes
KERNEL32! 77e7eb69()
[/code]
And here is a disassembly immediately surrounding the code that execution stops on.
[code]
77E778BE xor eax,eax
77E778C0 jmp 77E77554
77E778C5 mov ecx,dword ptr [esp+4]
77E778C9 mov eax,0FFFFFFFFh
77E778CE lock xadd dword ptr [ecx],eax
77E778D2 dec eax
77E778D3 ret 4
[/code]
It apparently stops on 77E778CE. It would appear that the pointer in ecx is a bad pointer (NULL or outside the program's memory space). I'm not 100% sure what lock xadd does, I googlged for it and it appears to have to do with threads and making it so another thread can't mess upo a counter of somekind. I'm wondering why it doesnt crash on 77E778C5... or is all that happens there is the value pointed to on the stack pointer+4 being put into ecx and that is used as a pointer? I'm not good with pointers in assembly (I don't know the syntax well enough) so I don't know if I really know what I'm talking about.
Either way it seems like it ends up with an invalid pointer. My biggest question is why? I have not done anything out of the ordinary. I followed all the examples I've found that use CFileDialog (even the one on MSDN) exactly as they were presently (sans a few minor changes that shouldn't cause an error like this). I don't understand why it used to work and now it doesn't (in any new or old project).
Any help on this would be grealty appreciated. I'm at a total loss here. This far exceeds my ability...
|
|
|
|
|
This seems very strange. One suggestion is check the packing of the project with one that works. There can be some quite strange side effects with packing. Normally projects default to 8 byte alignment.
Ant.
|
|
|
|
|
I Use them to Make Multilingual interface for my programm(LoadLibrary(),AfxSetResourceHandle()), But i want to have in my menu options to change interface dinamically. How to update all views after calling AfxSetResourceHandle()???
|
|
|
|
|
I'm a C/C++ programmer. I want to learn Window programming. One of my friends suggested me to study SDK (petzold's book) first and then move to MFC.He told me by reading petzold's famous book "progamming windows" I will have my basic concept regarding windows cleared.
now .Net has come. Does .net have changed the idea behind windows programming. so is it worth to read petzold first or directily move to VC++ .net?...i want to learn it as soon as possible...
plz help me....
|
|
|
|
|
Well, i cant say about .NET yet, but the petzold book is a worth buy. It will help you to grasp all of VC++ (though it doesnt include Directx, COM, etc). Petzold teaches you VC++ as WINDOWS API (not MFC ). "Windows API" is definitely a stronger way to program Windows. Also it provides much more flexibility than MFC.
If you want to master windows programming, this is the best book available in the market. Also once u have learned the API, u can easily learn MFC(concepts are mostly same).
COM, OLE ald things like that can be implemented better(easily) via MFC.
Go fo it....
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
|
|
|
|
|