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. File access in C++

File access in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancehelp
5 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.
  • D Offline
    D Offline
    Durga_Devi
    wrote on last edited by
    #1

    Hi, While reading the double value from the written text file, the memory get corrupetd.Here is the sample code and its output for the scenario. Can anyone help in this.

    #include ;
    #include "stdafx.h"

    int main(int argc, char* argv[])
    {
    double dbleArray[][4] = { -315.00000255999998,0.00000000000000000,0.00000000000000000,-166.19238000000001,
    -329.19238000000001,315.00000255999998,-329.19238000000001,-329.19238000000001,
    0.00000000000000000,0.00000000000000000,320.00000000000000,-536.00000000000000,
    0.00000000000000000,0.00000000000000000,0.00000000000000000,1.0000000000000000};

    //Write the data to the file
    FILE\* pFile = NULL;
    pFile = \_wfopen(L"C:\\\\Temp\\\\InputVolumeDetails.txt", L"w");
    if(pFile)
    {
    	for(int nRow=0; nRow<4; nRow++)
    	{
    		for(int nCol=0; nCol<4; nCol++)
    		{
    			fwrite(&dbleArray\[nRow\]\[nCol\], sizeof(double), 1, pFile);
    		}
    	}
    }
    fclose(pFile);
    
    //Read the data from the file
    double dbleArray\_r\[4\]\[4\];
    FILE\* pFile\_r = NULL;
    pFile\_r = \_wfopen(L"C:\\\\Temp\\\\InputVolumeDetails.txt", L"r");
    if(pFile\_r)
    {
    	for(int nRow=0; nRow<4; nRow++)
    	{
    		for(int nCol=0; nCol<4; nCol++)
    		{
    			fread(&dbleArray\_r\[nRow\]\[nCol\], sizeof(double), 1, pFile\_r);
    			printf("%f\\n",dbleArray\_r\[nRow\]\[nCol\]);
    		}
    	}
    }
    fclose(pFile);
    
    return 0;
    

    }

    The output for the above code is -315.000003 0.000000 0.000000 -92559631348757048000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000

    R L 2 Replies Last reply
    0
    • D Durga_Devi

      Hi, While reading the double value from the written text file, the memory get corrupetd.Here is the sample code and its output for the scenario. Can anyone help in this.

      #include ;
      #include "stdafx.h"

      int main(int argc, char* argv[])
      {
      double dbleArray[][4] = { -315.00000255999998,0.00000000000000000,0.00000000000000000,-166.19238000000001,
      -329.19238000000001,315.00000255999998,-329.19238000000001,-329.19238000000001,
      0.00000000000000000,0.00000000000000000,320.00000000000000,-536.00000000000000,
      0.00000000000000000,0.00000000000000000,0.00000000000000000,1.0000000000000000};

      //Write the data to the file
      FILE\* pFile = NULL;
      pFile = \_wfopen(L"C:\\\\Temp\\\\InputVolumeDetails.txt", L"w");
      if(pFile)
      {
      	for(int nRow=0; nRow<4; nRow++)
      	{
      		for(int nCol=0; nCol<4; nCol++)
      		{
      			fwrite(&dbleArray\[nRow\]\[nCol\], sizeof(double), 1, pFile);
      		}
      	}
      }
      fclose(pFile);
      
      //Read the data from the file
      double dbleArray\_r\[4\]\[4\];
      FILE\* pFile\_r = NULL;
      pFile\_r = \_wfopen(L"C:\\\\Temp\\\\InputVolumeDetails.txt", L"r");
      if(pFile\_r)
      {
      	for(int nRow=0; nRow<4; nRow++)
      	{
      		for(int nCol=0; nCol<4; nCol++)
      		{
      			fread(&dbleArray\_r\[nRow\]\[nCol\], sizeof(double), 1, pFile\_r);
      			printf("%f\\n",dbleArray\_r\[nRow\]\[nCol\]);
      		}
      	}
      }
      fclose(pFile);
      
      return 0;
      

      }

      The output for the above code is -315.000003 0.000000 0.000000 -92559631348757048000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000

      R Offline
      R Offline
      Romualdas Cukuras
      wrote on last edited by
      #2

      Try to change write and read modes to binary L"wb" and L"rb"

      1 Reply Last reply
      0
      • D Durga_Devi

        Hi, While reading the double value from the written text file, the memory get corrupetd.Here is the sample code and its output for the scenario. Can anyone help in this.

        #include ;
        #include "stdafx.h"

        int main(int argc, char* argv[])
        {
        double dbleArray[][4] = { -315.00000255999998,0.00000000000000000,0.00000000000000000,-166.19238000000001,
        -329.19238000000001,315.00000255999998,-329.19238000000001,-329.19238000000001,
        0.00000000000000000,0.00000000000000000,320.00000000000000,-536.00000000000000,
        0.00000000000000000,0.00000000000000000,0.00000000000000000,1.0000000000000000};

        //Write the data to the file
        FILE\* pFile = NULL;
        pFile = \_wfopen(L"C:\\\\Temp\\\\InputVolumeDetails.txt", L"w");
        if(pFile)
        {
        	for(int nRow=0; nRow<4; nRow++)
        	{
        		for(int nCol=0; nCol<4; nCol++)
        		{
        			fwrite(&dbleArray\[nRow\]\[nCol\], sizeof(double), 1, pFile);
        		}
        	}
        }
        fclose(pFile);
        
        //Read the data from the file
        double dbleArray\_r\[4\]\[4\];
        FILE\* pFile\_r = NULL;
        pFile\_r = \_wfopen(L"C:\\\\Temp\\\\InputVolumeDetails.txt", L"r");
        if(pFile\_r)
        {
        	for(int nRow=0; nRow<4; nRow++)
        	{
        		for(int nCol=0; nCol<4; nCol++)
        		{
        			fread(&dbleArray\_r\[nRow\]\[nCol\], sizeof(double), 1, pFile\_r);
        			printf("%f\\n",dbleArray\_r\[nRow\]\[nCol\]);
        		}
        	}
        }
        fclose(pFile);
        
        return 0;
        

        }

        The output for the above code is -315.000003 0.000000 0.000000 -92559631348757048000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631349317831000000000000000000000000000000000000000000000.000000

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        A very subtle situation which is a great lesson for the future. Both your read and write loops contain no error checking. However having completed the write processing, a visual check of the output file suggests that the content is correct. The read loop reads sixteen values and prints them out, but does not check that you have read the full values every time. If you take a look at the file produced by this program you will see the hex value 'x1a' at character position 26. This character is taken by the fread() function as signifying no more data, so the last value read in is incomplete which gives the strange value printed by the program. Every subsequent read will fail so the value stored in memory is whatever was there previously; in this case the values that happen to be in the array storage space. The solution is to always check the results of your read and write statements, but in this case you also need to use "wb" and "rb" on your _wfopen statements to ensure the 'x1A' is not taken as end of file.

        D 1 Reply Last reply
        0
        • L Lost User

          A very subtle situation which is a great lesson for the future. Both your read and write loops contain no error checking. However having completed the write processing, a visual check of the output file suggests that the content is correct. The read loop reads sixteen values and prints them out, but does not check that you have read the full values every time. If you take a look at the file produced by this program you will see the hex value 'x1a' at character position 26. This character is taken by the fread() function as signifying no more data, so the last value read in is incomplete which gives the strange value printed by the program. Every subsequent read will fail so the value stored in memory is whatever was there previously; in this case the values that happen to be in the array storage space. The solution is to always check the results of your read and write statements, but in this case you also need to use "wb" and "rb" on your _wfopen statements to ensure the 'x1A' is not taken as end of file.

          D Offline
          D Offline
          Durga_Devi
          wrote on last edited by
          #4

          Thanks for all your replies. I tried with "wb" and "rb" mode and its working fine.

          L 1 Reply Last reply
          0
          • D Durga_Devi

            Thanks for all your replies. I tried with "wb" and "rb" mode and its working fine.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Good news. But don't forget my comments about error checking, particularly when reading the file. Never assume that the file contents will be correct, however they were created.

            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