|
ok i think i got the problem. Update the function by calling the SeekToBegin() as follows
void CSRFToolDlg::OnBnClickedRead()<br />
{<br />
TCHAR tempBuffer[256] ;<br />
UINT readText = 0;<br />
openFile.SeekToBegin();<br />
readText = openFile.Read(tempBuffer, 256 ) ;<br />
SetDlgItemText(IDC_S_READ, tempBuffer ) ;<br />
}
|
|
|
|
|
Seems this is working fine. But one issue is there, if the string length is more than 256 characters. Can I set it to the length of the string which I've already written in the file.
Thanks
I appreciate your help all the time...
Eranga
|
|
|
|
|
ya... Either you can read the full text in one read( this will not be possible is the file is too big. because memeory allocation will fail for big length of data ) or read the text in a loop.
// For reading the complete text in single read
void CSRFToolDlg::OnBnClickedRead()<br />
{<br />
<br />
ULONGLONG nLength = openFile.GetLength();
TCHAR* ptempBuffer = new TCHAR[nLength];<br />
UINT readText = 0;<br />
openFile.SeekToBegin();<br />
readText = openFile.Read(tempBuffer, 256 ) ;<br />
SetDlgItemText(IDC_S_READ, ptempBuffer ) ;<br />
delete[] ptempBuffer;<br />
}
|
|
|
|
|
it seems that, naveen has entered the board means, the problem is solved.
good.
|
|
|
|
|
chandu004 wrote: it seems that, naveen has entered the board means, the problem is solved.
Thanks for the comment. But its not working always
|
|
|
|
|
but i like the way you tackle the problems.
really i appreciate you.
where are you locally from.
ap?
hyderabad?
|
|
|
|
|
No I am from kerala. But currently in Japan. Where r u from?
|
|
|
|
|
iam in hyderabad and working here since 2004.
my platform is vc++ 6.0,
and embedded software development in c.
these days, i have worked in communications and only communications.
|
|
|
|
|
In My Humble Opinion your basic understanding of Read() is incorrect. Read() will always read the number of characters given by the second argument or the total number of characters in the file, whichever is the smaller. What you really want is to do something like this.
CStdioFile fileOpen;
OnBnClickedOpen() can stay the same
void CSRFToolDlg::OnBnClickedWrite()
{
GetDlgItemText(IDC_S_WRITE, readString) ;
if (readString.Right(1) != _T("\n"))
readString += _T("\n");
AfxMessageBox("Data write to the file successfully", MB_OK) ;
openFile.WriteString(readString) ;
}
void CSRFToolDlg::OnBnClickedRead()
{
CString readText;
openFile.ReadString(readText) ;
SetDlgItemText(IDC_S_READ, readText) ;
}
Ohh and there is another potential problem, if you click Open, Write, Read then the result will not be what you expect because the OnBnClickedRead is not returning the filepointer to the start of the file
openFile.SeekToBegin();
you may also nead to call openFile.Flush() to ensure that buffers are flushed to disk before seeking to the begining of the file.
|
|
|
|
|
I have one thing pointed you. First one is the WriteString is not a member of CFile. So?
Actually later I've close the file. Until it is open and wont be issue, I suppose.
I appreciate your help all the time...
Eranga
|
|
|
|
|
ReadString WriteString are members of CStdioFile which is derived from CFile, hence the CStdioFile openFile;
|
|
|
|
|
I have one alternative,
if you are facing problems with CFile, then try the same with FILE *fp.
then i can guide you btter.
BTW, have you forwarded it to me?
|
|
|
|
|
Actually most familiar thing right now is CFile. I send the code to you.
I appreciate your help all the time...
Eranga
|
|
|
|
|
yah iam working on that.
but you would have posted me your zipped project folder(without debug folder)
it would have been easier for me to download it and see.
if possible, send me ur whole folder.
any way i will come back to u.
|
|
|
|
|
Ok, I'll send it to you
I appreciate your help all the time...
Eranga
|
|
|
|
|
As someone mentioned before, you are attempting to read from the same openFile, however your Write to that file left the file pointer at the end of the file, so your read is trying to read past the end.
What you need to add is
void CSRFToolDlg::OnBnClickedRead()
{
char tempBuffer[256] ;
UINT readText = 0;
openFile.SeekToBegin();
readText = openFile.Read(tempBuffer, strlen(tempBuffer)) ;
SetDlgItemText(IDC_S_READ, readText) ;
}
Hope that helps.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Eranga Thennakoon wrote: readText = openFile.Read(tempBuffer, strlen(tempBuffer)) ;
Why are you are passing 0 as the second argument to Read() ?
Eranga Thennakoon wrote: SetDlgItemText(IDC_S_READ, readText) ;
The second argument to SetDlgItemText() is supposed to be a null-terminated string, not a number.
"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
|
|
|
|
|
He only hopes it's 0. It looked uninitialized to me.
Is this thread a halloween joke? If so, you got me.
WTH?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It ended up being a bad design. He had buttons for opening, writing to, and reading from a file. He did not take into consideration the state of the file pointer after a write operation (or before a read operation).
"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
|
|
|
|
|
DavidCrow wrote: Why are you are passing 0 as the second argument to Read()?
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?
DavidCrow wrote: The second argument to SetDlgItemText() is supposed to be a null-terminated string, not a number.
I've not clear this. What I've got is readText for the SetDigItemText() should be a string, but here in my code it is a number.
I appreciate your help all the time...
Eranga
|
|
|
|
|
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
|
|
|
|