Read and write in file
-
Hi, You can use CFile, like this you open the file..
char* pszFileName = "c:\\test\\myfile.dat";
CFile myFile;
CFileException fileException;if ( !myFile.Open( pszFileName, CFile::modeCreate |
CFile::modeReadWrite, &fileException ) )like this you can read and write..
char szBuffer[256];
UINT nActual = 0;
CFile myFile;myFile.Write( szBuffer, sizeof( szBuffer ) );
myFile.Seek( 0, CFile::begin );
nActual = myFile.Read( szBuffer, sizeof( szBuffer ) );Like this you close the file..
myFile.Close();
The price of anything is the amount of life you exchange for it. Thanks and Regards. SANTHOSH V
-
CStdioFile::ReadString CStdioFile::WriteString
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
CStdioFile::ReadString CStdioFile::WriteString
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
i try both example but nothing is write in file..File is open but data is not write there? Plz help me
singh
show your code dude !
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
CStdioFile::ReadString CStdioFile::WriteString
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
show your code dude !
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Hi I more problem when new is came then old line is delete.But i don't want to delete old line..Plz help me
cpvc++ wrote:
Plz help me
F*CK, you don't reply to my questions, and still you're asking for help ?!!! :mad:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
cpvc++ wrote:
Plz help me
F*CK, you don't reply to my questions, and still you're asking for help ?!!! :mad:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
-
Mr. toxcct
I am not Mr. polop which is start this thread.I am different user and it's post help me and i want some more help me..Sorry for missunderstanding any way my code is here..Pla help me
CStdioFile file;
if (file.Open("\\log.txt", CFile::modeCreate|CFile::modeWrite ))
{
file.WriteString(logfile);file.WriteString("\\n"); } file.Close();
logfile is CString type which have some information.. Now i am able to write in file but old line is delete when new line write. Plz help me
-
Mr. toxcct
I am not Mr. polop which is start this thread.I am different user and it's post help me and i want some more help me..Sorry for missunderstanding any way my code is here..Pla help me
CStdioFile file;
if (file.Open("\\log.txt", CFile::modeCreate|CFile::modeWrite ))
{
file.WriteString(logfile);file.WriteString("\\n"); } file.Close();
logfile is CString type which have some information.. Now i am able to write in file but old line is delete when new line write. Plz help me
cpvc++ wrote:
I am not Mr. polop which is start this thread
sorry, I didn't notice that aspect of the things :) The problem in your code is that you're using
CFile::modeCreate
, but according to the documentation[^], this flag tells to truncate the file if already existing :MSDN says:
CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length
So, you have to add the flag
CFile::modeNoTruncate
to counter this undesired behavior. The use of this flag however keeps the file pointer at the beginning of the file. So if you want to append things at the end, you must call once afterCStdioFile::Open()
the functionCStdioFile::Seek(CFile::end)
. Does the following work better ?CStdioFile file;
if (file.Open(_T("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {
//file.Seek(0, CFile::end);
file.SeekToEnd();
file.WriteString(logfile);
file.WriteString(_T("\n"));
file.Close();
}[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
modified on Thursday, September 4, 2008 6:46 AM
-
cpvc++ wrote:
I am not Mr. polop which is start this thread
sorry, I didn't notice that aspect of the things :) The problem in your code is that you're using
CFile::modeCreate
, but according to the documentation[^], this flag tells to truncate the file if already existing :MSDN says:
CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length
So, you have to add the flag
CFile::modeNoTruncate
to counter this undesired behavior. The use of this flag however keeps the file pointer at the beginning of the file. So if you want to append things at the end, you must call once afterCStdioFile::Open()
the functionCStdioFile::Seek(CFile::end)
. Does the following work better ?CStdioFile file;
if (file.Open(_T("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {
//file.Seek(0, CFile::end);
file.SeekToEnd();
file.WriteString(logfile);
file.WriteString(_T("\n"));
file.Close();
}[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
modified on Thursday, September 4, 2008 6:46 AM
ok when i am useing your code then i am geting error..Plz help me
error C2660: 'CStdioFile::Open' : function does not take 1 arguments
error C2660: 'CStdioFile::Seek' : function does not take 1 arguments
error C2065: 'logfile' : undeclared identifier1>
error C2660: 'CStdioFile::Open' : function does not take 1 arguments
error C2660: 'CStdioFile::Seek' : function does not take 1 arguments
error C2065: 'logfile' : undeclared identifier -
ok when i am useing your code then i am geting error..Plz help me
error C2660: 'CStdioFile::Open' : function does not take 1 arguments
error C2660: 'CStdioFile::Seek' : function does not take 1 arguments
error C2065: 'logfile' : undeclared identifier1>
error C2660: 'CStdioFile::Open' : function does not take 1 arguments
error C2660: 'CStdioFile::Seek' : function does not take 1 arguments
error C2065: 'logfile' : undeclared identifiergod, I made a typo. you shouldn't have read
if (_T(file.Open("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {
but this :
if (file.Open(_T("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {
I fixed the previous post BTW. and sorry, I am at work, and I have no compiler, so I couldn't test it before posting...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
god, I made a typo. you shouldn't have read
if (_T(file.Open("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {
but this :
if (file.Open(_T("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {
I fixed the previous post BTW. and sorry, I am at work, and I have no compiler, so I couldn't test it before posting...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
i was read but i think both have make same mistake..And mistake is
file.Seek(CFile::end)
this is not right and right one is this
file.Seek(0,CFile::end)
Any way thxs for help..
cpvc++ wrote:
this is not right and right one is this file.Seek(0,CFile::end)
Yes you're right. BTW, you still can call
CFile::SeekToEnd()
which takes no parameters, and moves directly to the end of file. anyway, does it work now for you ?[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]