|
I did other example to prove somthing, I did :
CStdioFile f;
CString path= GetUserHomeDir() + _T("\\temporal.txt");
f.Open(path, CFile::modeCreate | CFile::modeWrite | CFile::typeText);
double a=9.8;
CString b=_T("hello");
f.WriteString(b);
f.Write(&a,sizeof(double));
f.Close();
I see the content from the file, and only wrote hello. This is the content from the file:
hello™™™™™#@
|
|
|
|
|
antonio343 wrote: I see the content from the file, and only wrote hello.
The above code shows you follow the word "hello" with a double value, so the file content is correct. You also do not show any code to read the file so I am still not sure what your problem is.
|
|
|
|
|
The content from the file isn't correct!!!!!
I wrote hello and 9.8 and it isn't the content of the file.
In adittion, I made a method to read the file
CStdioFile f;
CString b;
double a;
CString path= GetUserHomeDir() + _T("\\temporal.txt");
f.Open(path, CFile::modeRead | CFile::typeText);
f.ReadString(b);
f.Read(&a,sizeof(double));
f.Close();
And it dont read well. It read hello#@ and in the other variable, read rubbish
|
|
|
|
|
antonio343 wrote: The content from the file isn't correct!
Yes it is, you do not understand what you are writing. The line:
f.Write(&a,sizeof(double));
writes the double value exactly as it is held in memory as a binary value 8 bytes wide. If you wish to convert it to a textual representation then you need to format it into a text string with sprintf() or CString , using the appropriate format codes.
|
|
|
|
|
ok..
But I don't need to show.
In the first function that I post here, I need to copy the value of variable in a file, and after load the value in variable, I dont know how to do it, becouse I try with fread/fwrite, CFile class, CStdio class, and I don't get something.
The problem is the variable are several kind of dates, CString, double, int...
but I think that the main problem is save and load CString kind.
|
|
|
|
|
There is no problem with what you suggest, you merely need to ensure that what you try to read back exactly mirrors what you write. This means that you must read back the exact number of bytes that you have written in the first place. With elementary items (int , double , char etc.) this is not difficult, but with strings you need to add some extra information to know how many characters to read in; you could do this by adding an integer in front of the string that specifies its length. Alternatively, use one of the formatting functions to convert all your data to string items and use some form of field separation (e.g. CSV, XML) to write in lines of text which you can then reconvert when reading back.
|
|
|
|
|
I tried to do this:
int nLength = b.GetLength() + 1;
int nBytes = nLength * sizeof(TCHAR);
f.Write(&nLength, sizeof(int));
f.Write(b, nBytes);
f.Write(&a,sizeof(double));
And the problem is the same, this is the content of the file:
h o l a C a r a c o l a š™™™™™#@
|
|
|
|
|
"Here is the content of the file"...
Just how are you getting that output to show us? Is that from some editor or file dump utility?
If you write a file containing some binary data (and you do, both 'nLength' and 'a' are written to the file as plain binary data), and then you ask an editor to display the content of the file, of course you are going to get garbage.
Editors make the reasonable assumption that the entire data file is 'char' and will attempt to interpret the file in that way, rendering all the characters into some printable form, including trash characters as you observe.
For binary data, editors and other display programs are inappropriate for looking at the contents to see if it is correct. Raw binary file dumpers are the best for that, then you can see every byte.
|
|
|
|
|
So what is the problem? Do you think your binary values are going to somehow magically transform themselves to textual representations? As I said before this content is correct and if you read it back using the information that you used to write it out you will have no problems. Or, you could adopt my other suggestion of converting everything to strings and using CSV, XML or some other system to manage your data.
|
|
|
|
|
But the file isn't binary, it is text file, and dont show well the content
I'm sorry, you are right, the content is read well
Thank you so much, finally I get to do run well my function
modified 23-Oct-11 7:21am.
|
|
|
|
|
I have a dialog inherited from CResizableDialog, and some controls in it. I would like the user to be able to resize controls within dialog, like with some splitters. There are 4 major areas, 3 on top window half, and one on bottom. I thought that some splitter class from CP would help me here, but the code is either immature, or requires creation of panes. I don't have panes, just controls, and don't know how to work with panes. What is the recommended class, or best common practice, and where can I find some sample code?
|
|
|
|
|
You should not be using a CDialog for this. MFC has a CSplitterWnd that will handle multiple panes and you can add any other CWnd type within each pane.
|
|
|
|
|
Mihajlo Cvetanovic wrote: I would like the user to be able to resize controls within dialog That's a tall order.
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun
|
|
|
|
|
Hi Guru's,
I am having a service where i am using "WNetAddConnection2" method to use network resource like shared drive.
I would like to know if there is any other way where i can avoid using MPR.lib as additional dependencies , when i try to install the service it gives error saying MFC90.DLL not found reinstalling may fix the problem.
I have removed MPR.LIB related code and compiled the service it works fine for me,but i need to have those code as part of service.so is there any other way by which i can make it working.
if i dont use MPR.lib,it gives me linker error:
1>Linking...
1>Net.obj : error LNK2019: unresolved external symbol _WNetAddConnection2A@16 ...................................
Please let me know your inputs.
Thanks
Vikas
vikas da
|
|
|
|
|
I have never used this feature but there may be some clues in the documentation[^]. You can also find some help about MFC90.DLL here[^].
|
|
|
|
|
Thank you so much for the reply.
I had read the documentation..but this is not something coding problem i guess,
i missing some configuration may be redistribution pack or some DLL...
why i am guessing this this code was working fine with Visual Studio 2005,now i have upgrated to 2008 +some enhancement.
vikas da
|
|
|
|
|
Well the error messages you are seeing seem fairly clear. If you do not include MPR.lib you get a linker undefined issue, so you must include that library. With that library included you have a dependency on MFC90.dll, so you must get a copy of that dll installed on your system.
|
|
|
|
|
Thank you so much guys..i have fixed the linker error by editing project setting where Menifest was not included..now i am getting side by side asseble error for Microsoft.VC80.CRT...
vikas da
|
|
|
|
|
A similar issue, you need to check your settings: directories, libraries etc.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
|
Okay, I'm new here and new to the community...don't kill me if I seem lazy? I swear I'm not... x)
ANYWAYS.
Here is a template-abusive custom vector class. It implements buffering, even. (:
And Here and here is a custom vString class ( IDK what the 'v' stands for, sorry. Just seemed right. )
I have beat my brains out a few times trying to debug this...
Would anybody be willing to help me debug / better organize this code? { Or point out huge problems with it }
It just seems really unburly to me, and I'm having a hard time deciding if I should rely on the Vector<>::Add functionality for my vString::Insert() functions ( Append / Prepend are just convenience wrapping functions. I intend to make the vString class operate like jQuery. $().Append($().html("")).Prepend().Remove().Insert() etc )
I will also add support for using glibmm's regex library, but for now, I need to get the basics working out for me.
Any help would be much appreciated!
The roof's on fire, but keeps out the rain, so clench your fists and enjoy the pain.
|
|
|
|
|
** Also, when you see something like
if(sizeof(T)<=sizeof(T*)){
} else {
}
I'm aware that a T* is almost always going to be 4 bytes long, but I felt the need to place things this way. I'm also aware that most of my code is lacking heavily in comments. This is because I usually work on my own, and make code that seems self explanatory to me.
|
|
|
|
|
Since you are new, you may not be aware of the following points to keep in mind:
1. Nobody is going to download your code from a link in order to look at it
2. You need to have a very specific question, and post a very short snippet of the relevant code
3. Your request of "debug/organize my code" is far to broad and general for a forum such as this
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Oh, okay. My apologies!
class Range{
private:
int mStart,mSize,myID;
public:
Range(int aStart,int aSize):mStart(aStart),mSize(aSize){}
Range(Range& r):mStart(r.mStart),mSize(r.mSize){}
Range(const Range& r):mStart(r.mStart),mSize(r.mSize){} bool operator () (int x){
return x>=0? (x<mSize || mSize==0):(-x <= mSize || mSize==0);
}
int operator [] ( int x ) {
if(!operator()(x)) throw std::range_error("Range throwing exception; invalid indice number.");
return x + mStart;
}
int Size( ){ return mSize; }
int Size( int aSize ){ return mSize = aSize; }
int Start( ) { return mStart; }
int Start( int aStart ){ return mStart = aStart; }
};
Inside Vector<> Class:
Range All( int size = -1 ) {
if(size==-1) size = arraySize;
const Range ret(0,size);
return ret;
}
And the problematic code:
T& Add(const T& nElement, int indice = -1){
Allocate();
++arraySize;
Range ran = All();
indice = ran[indice];
if ( indice < 0 ) indice = arraySize + indice;
if(sizeof(T)<=sizeof(T*)){
if(static_cast<unsigned int>(indice+1)!=arraySize) memcpy(&(((T*)tElements)[indice+1]),&(((T*)tElements)[indice]),sizeof(T)*(arraySize-indice-1));
memcpy(&(((T*)tElements)[indice]),&nElement,sizeof(T));
return (((T*)tElements)[indice]);
} else {
memcpy( &(tElements[indice+1]),&(tElements[indice]),sizeof(T*)*(arraySize-indice));
if(construct) return *(tElements[indice] = new T(nElement));
else{
T* nPtr = (T*)new char[sizeof(T)];
memcpy(nPtr,&nElement,sizeof(T));
return *(tElements[indice] = nPtr);
}
}
}
More problematic code is:
vString& vString::Insert(char* str,Range& ran){
if(!str) throw std::runtime_error("vString::Insert(const char*,Range&) - Invalid first argument.");
int end = (ran.Size())?ran.Size():strlen(str)+1;
Allocate( end );
if(arraySize == 0 ) memcpy(&(((char*)tElements)[0]),str,arraySize = end);
else memcpy()
for(int i=0;i<end;i++,str++) Vector<char>::Add(*str,ran.Start());
arraySize--;
return *this;
}
vString& vString::Insert(const char* str,Range& ran){
if(!str) throw std::runtime_error("vString::Insert(const char*,Range&) - Invalid first argument.");
int end = (ran.Size())?ran.Size():strlen(str)+1;
Allocate( end );
if(arraySize == 0 ) memcpy(&(((char*)tElements)[0]),str,arraySize = end);
else{
Range& all = All();
int indice = ran.Start();
indice = all[indice];
if(indice<0)indice+=arraySize;
if(indice<0)indice=0;
memcpy(&(((char*)tElements)[indice+end-1]),&(((char*)tElements)[indice]),end-1)
}
arraySize--;
return *this;
}
Specific issues:
Not sure if I should rely on my Vector<>::Add() method to handle everything, or if I should make the vString::Insert() method take care of things.
I am also getting trouble getting my Prepend() method to perform expected results. It passes it's first argument as-is, then the second argument as a Range(0,0) ( 0 size = indefinite size, 0 start = first element ). Append() seems to work alright, but Prepend() seems to 'erase' the old string, and replace with only one character of the new string. ( I only test with the 'const char*' and const T& functions -- the char* overload was included to show you what the function used to look like )
The roof's on fire,
but keeps out the rain,
so clench your fists,
and enjoy the pain.
|
|
|
|
|
I'm going to be straight, this code is a mess, and as you've already noticed, error prone. Why not use std::vector<> and a couple of iterators? Problem solved.
?
|
|
|
|
|