|
Eranga Thennakoon wrote: No I'm try to send the string inside the buffer. As far as I got the idea, strlen finds the length of the string inside the buffer, is that right?
At the time you are calling Read() , tempBuffer is empty, thus strlen() will return 0 . The point I was trying to subtly make is that you want to use sizeof() not strlen() .
Eranga Thennakoon wrote: ...but here in my code it is a number.
Which is why it's wrong.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I'm done it this way now.
<br />
TCHAR tempBuffer[256];<br />
UINT len;<br />
<br />
openFile.SeekToBegin();<br />
<br />
openFile.Read(&len, sizeof(UINT));
<br />
if (len > 0)<br />
{<br />
openFile.Read(tempBuffer, len * sizeof(TCHAR)) ;<br />
tempBuffer[len] = _T('\0');<br />
}<br />
else<br />
{<br />
tempBuffer[0] = _T('\0');<br />
}<br />
<br />
SetDlgItemText(IDC_S_READ, tempBuffer) ;<br />
I appreciate your help all the time...
Eranga
|
|
|
|
|
Ya, openFile is a CFile object.
There in strlen(tempBuffer) what I'm trying to find is the length of the buffer which filed by the string. Is that wrong.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Eranga Thennakoon wrote: Is that wrong.
Yes, because strlen() will be called before Read() . You should really master the basics of C first before embarking on such a project. You'll save yourself, and others, a lot of grief.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Sure sir,
Still I'm learning, specially from you guys. If I've make any trouble with my silly questions it is really sorry.
I appreciate your help all the time...
Eranga
|
|
|
|
|
I'm not sure if you got a solution for this or not.
There's several problems here.
Thie biggest problem I see is that when you write strings to a file
you need a way to read them back. That means you need to know the
length of the string. In your code, you have no string size saved to the
file. That leads to code like this:
readText = openFile.Read(tempBuffer, strlen(tempBuffer)) ;
strlen() could return anything here, since tempBuffer is uninitialized.
Next, Read() returns the number of bytes read from the file, not a pointer to a string.
Passing readText to SetDlgItemText won't work, and it shouldn't even compile.
So back to the original problem...
First, I'm going to assume openFile is a CFile object. If that's the case, your
file is open in binary mode, which is going to give you problems in a Unicode
build. This is fine if you'll be using binary data in your file, but you need to
do all your string I/O in terms of bytes. That means you also need to store
the length of the string in the file so you know how many bytes to read back.
Here's an example of a simple fix, without changing your code much...
void CSRFToolDlg::OnBnClickedWrite()
{
CFile openFile;
GetDlgItemText(IDC_S_WRITE, readString) ;
UINT len = readString.GetLength();
openFile.Write(&len, sizeof(UINT));
if (len > 0)
openFile.Write((LPVOID)(LPCTSTR)readString, len * sizeof(TCHAR)) ;
AfxMessageBox("Data write to the file successfully", MB_OK) ;
}
void CSRFToolDlg::OnBnClickedRead()
{
TCHAR tempBuffer[256];
UINT len;
openFile.SeekToBegin();
openFile.Read(&len, sizeof(UINT));
if (len > 0)
{
openFile.Read(tempBuffer, len * sizeof(TCHAR)) ;
tempBuffer[len] = _T('\0');
}
else
{
tempBuffer[0] = _T('\0');
}
SetDlgItemText(IDC_S_READ, tempBuffer) ;
} This is a simple example, there's no error handling, and yes,
there's easier ways to do this, but hopefully it gives you some
clues about things to be aware of.
If you're reading/writing just string data, you may want to look into
using a CStdioFile class instead to take advantage of it's text related
methods.
Hope this helps,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks, seems now I'm getting better and better with all of your guys suggestions.
But the following part is not clear, when I do it it wont print any value to the file. Just print an annoying character.
Mark Salsbery wrote: openFile.Write(&len, sizeof(UINT)); // write the string length (in TCHARs)
I appreciate your help all the time...
Eranga
|
|
|
|
|
come on eranga, all this is confusing because, the thread length has got very big.
i think, you have got some clues right, so you must have solved some of your problems and got some more.
so create a new thread, and state that, with reference to the this thread, and specify your new problems.
we shall help you with fresh mind.
gud luck.
and btw, you didnt reply weather u got your solution, with my earlier suggestion. i.e. CFile returns the size of the file.
|
|
|
|
|
Ya, this is going to be a long thread. I've do this up to now. Find the number bytes and use it to read the file as follows.
<br />
void CSRFToolDlg::OnBnClickedWrite()<br />
{<br />
GetDlgItemText(IDC_S_WRITE, readString) ;<br />
UINT len = readString.GetLength() ;<br />
openFile.Write(&len, sizeof(UINT)) ; <br />
if (len > 0)<br />
{<br />
openFile.Write((LPVOID)(LPCTSTR)readString, len * sizeof(TCHAR)) ;<br />
}<br />
AfxMessageBox("Data write to the file successfully", MB_OK) ;<br />
}<br />
<br />
<br />
void CSRFToolDlg::OnBnClickedRead()<br />
{<br />
TCHAR tempBuffer[256];<br />
UINT len;<br />
<br />
openFile.SeekToBegin();<br />
<br />
openFile.Read(&len, sizeof(UINT)); <br />
<br />
if (len > 0)<br />
{<br />
openFile.Read(tempBuffer, len * sizeof(TCHAR)) ;<br />
tempBuffer[len] = _T('\0');<br />
}<br />
else<br />
{<br />
tempBuffer[0] = _T('\0');<br />
}<br />
<br />
SetDlgItemText(IDC_S_READ, tempBuffer) ;<br />
}<br />
Here the issue is, the length of the string is nor write properly to the file. You have my full code, try those two functions there. you will see what I'm saying.
After solving this I'll start a new one for next cases.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Eranga Thennakoon wrote: But the following part is not clear, when I do it it wont print any value to the file. Just print an annoying character.
You missed my point entirely.
You need some way to determine how long the string is.
You can't just call strlen() on an uninitialized char array.
If the string is the entire file use chandu004's suggestion -
you can read it all in one shot.
Otherwise, you're going to need to learn about "strings"
(char arrays), files, etc. before you'll understand any of this.
I gave one example that writes the string length so you know how much
to read later. Your CFile is in binary mode - I wrote binary data.
The "annoying character" was never meant to be a readable character.
If everything in the file needs to be text, you need to deal with that.
Whether you use endlines, carriage-return/linefeeed pairs, or some other
escape character to separate your strings is up to you.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I have more than 50 Edit boexs in a form and I want to access them in a loop rather than accessing one by one, Is there any way by which I can do it?
Thanks,
Anand.
|
|
|
|
|
Each editbox has an ID if you set these ids for example of 100 to 150 on the resource.h you can use of a loop.
|
|
|
|
|
Yeah right, but the project has more than 50 forms and i have to do this for all forms, already the resource IDs are tightly bound ... more , it is not reliable to .. So i was thinking if some generic loop by which we can access all the controls on form and then screen out the other controls.
Thanks,
Anand.
|
|
|
|
|
Anand Todkar wrote: Yeah right, but the project has more than 50 forms
Do all the forms have a common main dialog?
|
|
|
|
|
Anand Todkar wrote: ...already the resource IDs are tightly bound ... more , it is not reliable to...
What exactly do you mean by this? Why can't you just open the project's resource.h file (or whatever you have named it) and renumber the controls such that they are sequential?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Yeah David, you are right, i done the changes in Resource file and accessed the Controls in a loop.
Thanks for your help.
Thanks,
Anand.
|
|
|
|
|
You mean something like that,
#define IDC_ONE 1000<br />
#define IDC_TWO 1001
So, there can I use any number. I mean there is no restrictions on predefined events?
I appreciate your help all the time...
Eranga
|
|
|
|
|
Not any number,numbers must unique.
|
|
|
|
|
Ya, its' true. What I mean that there is any numbers couldn't I use, may be those numbers are already use somewhere in the application.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Anand Todkar wrote: Is there any way by which I can do it?
Set the taborder of all EditBox in a sequence. Now take the handle of EditBox in the top of zorder and call the GetWindow() with GW_HWNDNEXT. This will return the handle to the 2nd EditBox. now pass the 2nd EditBox handle to get third list and so on.
Another option is to set the id value of the EditBox in a sequence. Now call the GetDlgItem() function in a for loop with the index starting from the id of first EditBox to the last one.
|
|
|
|
|
Naveen.R wrote: Set the taborder of all EditBox in a sequence. Now take the handle of EditBox in the top of zorder and call the GetWindow() with GW_HWNDNEXT. This will return the handle to the 2nd EditBox. now pass the 2nd EditBox handle to get third list and so on.
This sounds good, let me try this way, as the Taborder is not going to change.
Thanks naveen
Thanks,
Anand.
|
|
|
|
|
Hi,
I made it very easy with an Array from pointers to the control. In my case, it was:
CMyColorComboBox* m_aComboBoxes [MAX];
then, in the constructor of the Dialog... I wrote the access to them.
m_aComboBoxes[0] = &m_cmcbbVisIn1;
m_aComboBoxes[1] = &m_cmcbbVisIn2;
m_aComboBoxes[2] = &m_cmcbbVisIn3;
m_aComboBoxes[3] = &m_cmcbbVisIn4;
and then I use loops to access, write, delete data from them.
for (int i = 0; i < MAX; i++)
m_aComboBoxes [MAX]->ResetContent ();
I have done it in that way because I had the member variables from class wizard. But you can use the idea as well without having any variable and using GetDlgItem (ID). If you use the GetDlgItem ... then you must use OnInitDialog or on InitialUpdate. Because if you use the constructor to give the pointers... then you will have an assertion because the window doesn't exist when trying to access.
Hope it helps
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Hi All,
I am using list class in my project(MFC Application). I have made list like .....
typedef std::list<rect> rectlist; // Here list is of type RECT
in one header file. And during my project execution I am storing rectangles in the list.
And now,When I use list iterator in another class like below....( I have already included its header file in using class)
rectlist::iterator i;
And i use this iterator like below in any other function......
for (i = rects.begin(); i != rects.end(); i++)
{
CaptureScreen(*i, m_mainbuff); // Error is here
}
I got error msg like ...
list iterator not dereferencable
at runtime in debug assertion Failed Box.
Thanks in Advance...
Ashish
-- modified at 1:10 Tuesday 16th October, 2007
|
|
|
|
|
it may because of using *i in argument.
solution is try to give appropriate argument in function calling then copy it in to original list.
or it may required type casting..
give me the prototype of CartureScreen( ) function.
|
|
|
|
|
Prototype of CaptureScreen()...
void CaptureScreen(RECT &UpdateArea, BYTE *scrBuff);
i have just used this ....
<br />
rectlist::iterator i;<br />
<br />
for (i = rects.begin(); i != rects.end(); i++)<br />
{<br />
CaptureScreen(*i, m_mainbuff);<br />
}<br />
<br />
But in my case it does not go into loop even if condition is true. And if I make..
i == rects.end()
then it goes into the loop but their values are different.And if it goes into loop then it gives error as I explained above.And if I make it like....
<br />
CaptureScreen((RECT &)i, m_mainbuff);<br />
<br />
then it does not give error.But when for is incremented then again it gives error like....
list iterator is not incremental
That's all the case....
Thanks Hiru.
|
|
|
|