|
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
|
|
|
|
|
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++
|
|
|
|