Click here to Skip to main content
15,917,862 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Read a file on click event Pin
CodingLover16-Oct-07 0:46
CodingLover16-Oct-07 0:46 
GeneralRe: Read a file on click event Pin
Naveen16-Oct-07 3:32
Naveen16-Oct-07 3:32 
GeneralRe: Read a file on click event Pin
chandu00416-Oct-07 1:36
chandu00416-Oct-07 1:36 
GeneralRe: Read a file on click event Pin
Naveen16-Oct-07 3:35
Naveen16-Oct-07 3:35 
GeneralRe: Read a file on click event Pin
chandu00416-Oct-07 4:12
chandu00416-Oct-07 4:12 
GeneralRe: Read a file on click event Pin
Naveen16-Oct-07 5:12
Naveen16-Oct-07 5:12 
GeneralRe: Read a file on click event Pin
chandu00416-Oct-07 20:49
chandu00416-Oct-07 20:49 
AnswerRe: Read a file on click event Pin
Roger Broomfield15-Oct-07 21:36
Roger Broomfield15-Oct-07 21:36 
GeneralRe: Read a file on click event Pin
CodingLover15-Oct-07 21:54
CodingLover15-Oct-07 21:54 
GeneralRe: Read a file on click event Pin
Roger Broomfield15-Oct-07 22:00
Roger Broomfield15-Oct-07 22:00 
GeneralRe: Read a file on click event Pin
chandu00415-Oct-07 22:01
chandu00415-Oct-07 22:01 
GeneralRe: Read a file on click event Pin
CodingLover15-Oct-07 22:17
CodingLover15-Oct-07 22:17 
GeneralRe: Read a file on click event Pin
chandu00415-Oct-07 22:31
chandu00415-Oct-07 22:31 
GeneralRe: Read a file on click event Pin
CodingLover15-Oct-07 22:41
CodingLover15-Oct-07 22:41 
AnswerRe: Read a file on click event Pin
krmed16-Oct-07 1:46
krmed16-Oct-07 1:46 
QuestionRe: Read a file on click event Pin
David Crow16-Oct-07 2:33
David Crow16-Oct-07 2:33 
AnswerRe: Read a file on click event Pin
Mark Salsbery16-Oct-07 5:53
Mark Salsbery16-Oct-07 5:53 
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]

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.