CFile writes garbage value
-
Im writing some data in to file. For that, im taking values and assign it to the struct varaible and then i add that sruct to CPtrList. Then im writing this CPtrList in file. But it writes some garbage value. I dont know where im doing wrong. Assigning values to struct:
sLbl= new SGraphLbl; sTagName = (CString)czTagGetVal; int pos = sTagName.Find("\\n"); sName = sTagName.Left(pos-1); if(sName.Right(1) == "Y") { sTagName = sName.Left(sName.GetLength()-2); sTagName.TrimLeft(); sTagName.TrimRight(); sLbl->sTagName = sTagName; sLbl->LblNo = Cnt; sLbl->iTagInd = Cnt; sLbl->PageNo = iPage; } LblList\[iPage\].AddTail(sLbl);
I am writing in to file in a seperate function
void Listtofile()
{
CFile gpFile;
if (!gpFile.Open( sFileName,CFile::modeCreate|CFile::modeWrite))
{
AfxMessageBox( sFileName + (CString)" - File Write Error");
return;
}
for(int iPage =0; iPageIt writes some thing if file and also i cannot read the file also.
Anu
-
Im writing some data in to file. For that, im taking values and assign it to the struct varaible and then i add that sruct to CPtrList. Then im writing this CPtrList in file. But it writes some garbage value. I dont know where im doing wrong. Assigning values to struct:
sLbl= new SGraphLbl; sTagName = (CString)czTagGetVal; int pos = sTagName.Find("\\n"); sName = sTagName.Left(pos-1); if(sName.Right(1) == "Y") { sTagName = sName.Left(sName.GetLength()-2); sTagName.TrimLeft(); sTagName.TrimRight(); sLbl->sTagName = sTagName; sLbl->LblNo = Cnt; sLbl->iTagInd = Cnt; sLbl->PageNo = iPage; } LblList\[iPage\].AddTail(sLbl);
I am writing in to file in a seperate function
void Listtofile()
{
CFile gpFile;
if (!gpFile.Open( sFileName,CFile::modeCreate|CFile::modeWrite))
{
AfxMessageBox( sFileName + (CString)" - File Write Error");
return;
}
for(int iPage =0; iPageIt writes some thing if file and also i cannot read the file also.
Anu
I don't know if this solves your problem, but this code may fail:
sTagName = (CString)czTagGetVal;
int pos = sTagName.Find("\n");
sName = sTagName.Left(pos-1);pos
is -1 when the string did not contain a line feed. If it contains multiple line feeds, all charaters at and right of it are removed. You are using a CString. So this may be better (removes also carriage returns):sName.Remove('\n');
sName.Remove('\r'); -
Im writing some data in to file. For that, im taking values and assign it to the struct varaible and then i add that sruct to CPtrList. Then im writing this CPtrList in file. But it writes some garbage value. I dont know where im doing wrong. Assigning values to struct:
sLbl= new SGraphLbl; sTagName = (CString)czTagGetVal; int pos = sTagName.Find("\\n"); sName = sTagName.Left(pos-1); if(sName.Right(1) == "Y") { sTagName = sName.Left(sName.GetLength()-2); sTagName.TrimLeft(); sTagName.TrimRight(); sLbl->sTagName = sTagName; sLbl->LblNo = Cnt; sLbl->iTagInd = Cnt; sLbl->PageNo = iPage; } LblList\[iPage\].AddTail(sLbl);
I am writing in to file in a seperate function
void Listtofile()
{
CFile gpFile;
if (!gpFile.Open( sFileName,CFile::modeCreate|CFile::modeWrite))
{
AfxMessageBox( sFileName + (CString)" - File Write Error");
return;
}
for(int iPage =0; iPageIt writes some thing if file and also i cannot read the file also.
Anu
Anu_Bala wrote:
gpFile.Write(Pd,sizeof( struct SGraphLbl));
Since your
struct
contains a reference to aCString
object, the above line is not the right way to serialize it (you're just writing the address of theCString
to the file, while yout should write its content).If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]