Click here to Skip to main content
15,911,531 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 
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) ;
// ensure the string is terminated with a newline
// so that ReadString will know when to stop reading
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) ;
// the trailing newline character that we appended before the WriteString
// is always removed by ReadString
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.
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 

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.