Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CFile writes garbage value

CFile writes garbage value

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anu_Bala
    wrote on last edited by
    #1

    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; iPage

    It writes some thing if file and also i cannot read the file also.

    Anu

    J CPalliniC 2 Replies Last reply
    0
    • A Anu_Bala

      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; iPage

      It writes some thing if file and also i cannot read the file also.

      Anu

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      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');

      1 Reply Last reply
      0
      • A Anu_Bala

        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; iPage

        It writes some thing if file and also i cannot read the file also.

        Anu

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Anu_Bala wrote:

        gpFile.Write(Pd,sizeof( struct SGraphLbl));

        Since your struct contains a reference to a CString object, the above line is not the right way to serialize it (you're just writing the address of the CString 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]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups