File access in C++
-
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
-
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
Try to change write and read modes to binary L"wb" and L"rb"
-
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
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.
-
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.
Thanks for all your replies. I tried with "wb" and "rb" mode and its working fine.
-
Thanks for all your replies. I tried with "wb" and "rb" mode and its working fine.