Click here to Skip to main content
15,903,203 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Read a file on click event Pin
David Crow16-Oct-07 6:01
David Crow16-Oct-07 6:01 
AnswerRe: Read a file on click event Pin
CodingLover16-Oct-07 18:00
CodingLover16-Oct-07 18:00 
GeneralRe: Read a file on click event Pin
David Crow17-Oct-07 4:00
David Crow17-Oct-07 4:00 
GeneralRe: Read a file on click event Pin
CodingLover18-Oct-07 20:08
CodingLover18-Oct-07 20:08 
GeneralRe: Read a file on click event Pin
CodingLover16-Oct-07 18:09
CodingLover16-Oct-07 18:09 
GeneralRe: Read a file on click event Pin
David Crow17-Oct-07 4:02
David Crow17-Oct-07 4:02 
GeneralRe: Read a file on click event Pin
CodingLover18-Oct-07 20:16
CodingLover18-Oct-07 20:16 
AnswerRe: Read a file on click event Pin
Mark Salsbery16-Oct-07 6:20
Mark Salsbery16-Oct-07 6:20 
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));    // write the string length (in TCHARs)
    if (len > 0)
        openFile.Write((LPVOID)(LPCTSTR)readString, len * sizeof(TCHAR)) ; // Note conversion of length in TCHARs to length in bytes
    AfxMessageBox("Data write to the file successfully", MB_OK) ;
}



void CSRFToolDlg::OnBnClickedRead()
{
    TCHAR tempBuffer[256];
    UINT len;

    openFile.SeekToBegin();

    openFile.Read(&len, sizeof(UINT));    // read the string length (in TCHARs)

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

Java | [Coffee]

GeneralRe: Read a file on click event Pin
CodingLover16-Oct-07 19:11
CodingLover16-Oct-07 19:11 
GeneralRe: Read a file on click event Pin
chandu00416-Oct-07 20:58
chandu00416-Oct-07 20:58 
GeneralRe: Read a file on click event Pin
CodingLover16-Oct-07 21:08
CodingLover16-Oct-07 21:08 
GeneralRe: Read a file on click event Pin
Mark Salsbery17-Oct-07 5:21
Mark Salsbery17-Oct-07 5:21 
QuestionHow to access all the Editboxes in a form. Pin
Anand Todkar15-Oct-07 19:12
Anand Todkar15-Oct-07 19:12 
AnswerRe: How to access all the Editboxes in a form. Pin
Hamid_RT15-Oct-07 19:19
Hamid_RT15-Oct-07 19:19 
GeneralRe: How to access all the Editboxes in a form. Pin
Anand Todkar15-Oct-07 19:24
Anand Todkar15-Oct-07 19:24 
GeneralRe: How to access all the Editboxes in a form. Pin
Naveen15-Oct-07 19:28
Naveen15-Oct-07 19:28 
QuestionRe: How to access all the Editboxes in a form. Pin
David Crow16-Oct-07 2:23
David Crow16-Oct-07 2:23 
AnswerRe: How to access all the Editboxes in a form. Pin
Anand Todkar16-Oct-07 2:34
Anand Todkar16-Oct-07 2:34 
GeneralRe: How to access all the Editboxes in a form. Pin
CodingLover15-Oct-07 19:26
CodingLover15-Oct-07 19:26 
GeneralRe: How to access all the Editboxes in a form. Pin
Hamid_RT15-Oct-07 19:38
Hamid_RT15-Oct-07 19:38 
GeneralRe: How to access all the Editboxes in a form. Pin
CodingLover15-Oct-07 19:47
CodingLover15-Oct-07 19:47 
AnswerRe: How to access all the Editboxes in a form. Pin
Naveen15-Oct-07 19:25
Naveen15-Oct-07 19:25 
GeneralRe: How to access all the Editboxes in a form. Pin
Anand Todkar15-Oct-07 19:31
Anand Todkar15-Oct-07 19:31 
AnswerRe: How to access all the Editboxes in a form. Pin
Nelek15-Oct-07 21:03
protectorNelek15-Oct-07 21:03 
QuestionList iteration Problem [modified] Pin
ashishbhatt15-Oct-07 19:04
ashishbhatt15-Oct-07 19:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.