|
Hi,
I need a simple and small class that I can serialize to disk (using fstream in binary mode). In the class I have a Standard C++ library string member, but after pumping objects of this class to disk I lost the string value on reading back. The class is something like this;
class Person
{
public:
string sName; (private or not doesn't matter)
int nAge;
Person(const string&Name, int nAgeOfPerson);
string GetName() {return sName;};
int GetAge() {return nAge};
}
and the ctor,
Person(const string&Name, int nAgeOfPerson)
{
sName = Name; //tried both, don't work.
sName = Name.substr(); //using initializing list also not!
nAge = nAgeOfPerson;
}
For my class, sizeof tell me the object is alway and only 28 bytes, even when the name is 29 characters long. I have no problem getting the age back. I guess when I write out to the file the name characters were somehow lost. I want to use Standard C++ Lib and using string but I don't know enough .
If I go back the old way using a char szName[50], for example, then everything is OK. But I really don't want to assign 50 bytes for every attribute in the class (small is beautiful for me here). Can someone tell me it is possible to force a value into the string member sName?
I am not sure if I make the point clear. Thanks in advance.
Will
|
|
|
|
|
The problem is that string stores the string you allocated on the heap, not within the class itself. The class only has a pointer to the string, thus when you write the binary contents of the class (including the string) to disk it saves the pointer to string as it exists right now, but when you reload that data, that data doesn't exist anymore, and thus the pointer is pointing off into nowhere.
What you are doing is called "bitwise" persistance, which means it saves the data bit for bit. What you want is called "deep" persistance. And to do this, you have to develop a mechanism to differentiate different types of data in your persistance file.
The easiest way to do this is to store the data as text, using the insertion operators of fstream:
(assumes using namespace std)
void person::opersist(fstream& out)
{
out << sName << endl << nAgeOfPerson << endl;
}
void person::ipersis(fstream& in)
{
in >> sName >> nAgeOfPerson;
}
|
|
|
|
|
i think u r taking in wrong way.
I have to read and write on the contents tab(which appears when u rigth click on any word file and select the properties).i am able to read and write rest all the tabs like (General,Summary,Statstic and custom) but not on contents. I am using IPropertySetStorage. the code i am using is given below please suggest some solution.........
Pankaj Mongia
|
|
|
|
|
Hello,
I created a listbox in a dialog-based application. I already changed (programmatically) the background color of the listbox, and also the text color of its items. But now, I would like to change the highlite color from the default windows color (blue). I would appreciate any help or ideas.
Thank you.
-Jake
|
|
|
|
|
|
Not an easy job. You'll have to create an owner drawn listbox and handle all the drawing and highligting yourself.
|
|
|
|
|
Give me a yell in a week or so to remind me to post my over-blown CListCtrl class. It's abhorantly huge and bloated but has most of the tricks you would ever need to work with a CListCtrl (and works with pre 4.71 common controls DLL).
cheers,
Chris Maunder
|
|
|
|
|
See http://www.codeproject.com/combobox/disableditemslistbox.asp as a starting point. You can ignore the "is disabled" logic and do your custom handling of WM_DRAWITEM, depending on whether the item is selected.
/ravi
"There is always one more bug..."
ravib@ravib.com
www.ravib.com
|
|
|
|
|
We have developed a COM object in VC++ and the graphical interface in VB.
We have defined in VB a variant array where will be stored the value returned by a VC++ function which is also a Variant.
The problem is that we get a “Type mismatch” error when an empty value is returned.
Can anybody help us? How would you do it?
Below you can find our code:
VB code
Dim MyArray() As Variant
MyArray = COMObject.MyFunctionInVC(CLng(IntData), IntData)
VC code
VARIANT MyClass::MyFunctionInVC (long ldata, SHORT ndata)
{
VARIANT vaResult;
VariantInit(&vaResult);
If (conditions)
{
……………
while (more conditions){
COleVariant vOle(csline, VT_BSTR);
safRet.PutElement(index, &vOle);
index[0]++;
}
vaResult=safRet.Detach();
}
else
{
COleVariant vOle("", VT_BSTR);
vaResult=vOle.Detach();
}
return vaResult;
}
|
|
|
|
|
ewhh.. that's just nasty.
C++ COM methods return HRESULT not VARIANT. If you want to return a variant array to your VB code, then you will need to define your method like the following:
IDL:
interface IMyClass : IDispatch
{
[id(1), helpstring("method MyFunctionInVC")] HRESULT MyFunctionInVC([in]long lngData, [in]short nData, [out,retval] VARIANT** ppVariantArray);
};
C++ Code:
STDMETHODIMP CMyClass::MyFunctionInVC(long lngData, short nData, VARIANT **ppVariantArray)
{
return S_OK;
}
|
|
|
|
|
Does anybody know how to alter the size of a Floating control window.
The obvious MoveWindow and SetWindowPos have no effect.
TIA
Norm
|
|
|
|
|
Silly suggestion time.
Have you tried MoveWindow() and then watched to see if the window received the
events and then ignored them. Some time ago (year or more) I had to deal with
a toolbar problem that MFC didn't handle. Turned out it was a nightmare because
MFC had all sorts of code that made blanket assumptions about behaviour and
forced certain things to happen even if you were trying to do the opposite.
Positioning and sizing problems were the main issue. I don't know if this
helps, but maybe it'll give you a pointer to find which part of MFC is ruining
your day. Try looking for WindowSizing and WindowSized events.
All that said, I like MFC, up to a point.
Stephen Kellett
|
|
|
|
|
I've got the pointer to the CMiniDockFrameWnd after the window was floated. Somewhere in the bowels of MFC, the window refuses to size after movewindow and setwindowpos have been thrown at it, its very hard to step thru the code, resize caused the code to fly everwhere.
The reason behind the question is that I want to roll out the sliding docking windows seen in Visual Studio.Net.
Anway back to operating on the bowels of MFC...
|
|
|
|
|
Dear all,
I am having some problem to use the AVICap macros provided by Windows. I am writing an application to constantly (say 1 second) grab an image from the web camera and save it as an bitmap file. I used capFileSaveDIB() to save the image captured into the bitmap file. The program works fine when it is the active window (running at the foreground). If the application is minimized, or covered by another application, which means the preview window is invisible, then, capFileSaveDIB() will not save the current image to the file.
Can anybody help me on this? Should I use capFileSaveDIB() for this? Or any setting that I have forgotten?
Thanks
Yong Haur
|
|
|
|
|
it is the major disadvantage of avicap class.it doesn't work in invisible mode.read article by vadim in multimedia/video section of code guru.try use his class it is slow but works fine
bye
bobbies_2001@yahoo.com
bobby
|
|
|
|
|
How I can place FileDialog in a view?
I don't want the fileDialog to pop-up when a button is pressed , I want the filedialog on the view when the view is opened.
|
|
|
|
|
The short answer is form view, but t could get ugly - some common dialogs do not function unless called modally.
Christian
The content of this post is not necessarily the opinion of my yadda yadda yadda.
To understand recursion, we must first understand recursion.
|
|
|
|
|
I seem to recall seeing an MS KB article about embedding common dialogs in other windows (eg, property sheets) and the jist of the article was that it can't be done because the dialogs expect to be modal. Search the KB - the article shouldn't be too tough to find.
--Mike--
http://home.inreach.com/mdunn/
I'm finger-lickin' good!
|
|
|
|
|
In a program i'm currently working on, i want to be able to zoom in bitmaps. I've created a memory dc containing the bitmap and every time the user clicks the zoom button this dc is stretch bitblitted in another memorydc called zoomdc. ZoomedPictureDC is written to the screen in ondraw(). This way I don't have to enlarge the picture every draw, saves a lot of speed. The following code is in the zoom function. ZoomedPictureDC is a member variable of the View.
ZoomedPictureDC.StretchBlt(0,0,pDoc->bmPicture.bmWidth * zoomlevel, pDoc->bmPicture.bmHeight * zoomlevel, &PictureDC,0,0,pDoc->bmPicture.bmWidth, pDoc->bmPicture.bmHeight,SRCCOPY);
Why doesn't this work? I tried stretchblitting the picture every time in ondraw, this does work but is terribly slow.
wavk
|
|
|
|
|
have you obtained a device contect to the window in your drawing function?
can't see one from the snippet you posted
---
"every year we invent better idiot proof systems and every year they invent better idiots"
|
|
|
|
|
When you say does not work, what does it do ? If nothimg, have you created the DC in question and selected a bitmap of suitable size into it ? If a and not b, you have a 1x1x1 bitmap space to work with. Does it look funny ? You may have forgotten to SetStretchBltMode ( to HALFTONE or COLORONCOLOR ). Did you do all this and nothing happened still ? There is a known bug in StretchBlt, where it returns 0 when it fails. When you use StretchBlt you should always check for a 0 return and deal with potential failure. My experience is it fails after a 16 x zoom, so I doubt that is the problem here.
Christian
The content of this post is not necessarily the opinion of my yadda yadda yadda.
To understand recursion, we must first understand recursion.
|
|
|
|
|
Type Mémo in MDB->0 to 65535 char and with CRecordSet insertion Failed with CString > 255 char
Please, Help.
|
|
|
|
|
I am unable to use string variable
I have:
#include <string>
but still when I write :
string filename;
I get error that :
missing ; before indentifier filename
Also I am unable to use vector
despite that fact that I have
#include <vector>
I am using Visual C++ 6.0
Please help me !!!!!!!!!!!!!!!!!!!1
|
|
|
|
|
Are you trying to use STL string? Probably you should add:
using namespace std;
after the #include
|
|
|
|
|
I have
#include < string >
using namespace std;
......................
............................
string filename;
cin>>filename;
I get following error :
: error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char="">,class std::allocator<char> >' (or there is no acceptab
le conversion)
Why ???
|
|
|
|