writing and reading BYTE array
-
Hi, How to write a BYTE array into a file and reading it back from. I used fwrite and fread but it is not working. Regards,
-
Hi, How to write a BYTE array into a file and reading it back from. I used fwrite and fread but it is not working. Regards,
subramanyeswari wrote:
I used fwrite and fread but it is not working.
It should. Please, when you have a problem like describe exactly what you did so that we can check it. Post some code how you open the file and read/write in it.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ -
subramanyeswari wrote:
I used fwrite and fread but it is not working.
It should. Please, when you have a problem like describe exactly what you did so that we can check it. Post some code how you open the file and read/write in it.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++Here is the code
char strPasswd[8] = "test123";
DWORD dlen = 8;
size_t t ;
long lSize;size\_t result; BYTE\* Newpasswd; FILE \*fp = fopen("c:\\\\test.bin", "wb+"); BYTE\* encodedpasswd = EncodeString(strPasswd,&dlen); t = fwrite((unsigned char\*)encodedpasswd,1,sizeof((unsigned char\*)encodedpasswd),fp); fclose(fp); fp = fopen("c:\\\\test.bin", "rb+"); fseek (fp , 0 , SEEK\_END); lSize = ftell (fp); rewind (fp); Newpasswd= (unsigned char\*) malloc (sizeof(unsigned char)\*lSize); int i = fread(Newpasswd,1,sizeof(Newpasswd),fp);//Newpasswd is not the same as encodedpasswd char\* tempPassword = DecodeString(testpasswd,&dlen);// here tempPassword is junk
Is writing is not done properly? Regards,
-
Here is the code
char strPasswd[8] = "test123";
DWORD dlen = 8;
size_t t ;
long lSize;size\_t result; BYTE\* Newpasswd; FILE \*fp = fopen("c:\\\\test.bin", "wb+"); BYTE\* encodedpasswd = EncodeString(strPasswd,&dlen); t = fwrite((unsigned char\*)encodedpasswd,1,sizeof((unsigned char\*)encodedpasswd),fp); fclose(fp); fp = fopen("c:\\\\test.bin", "rb+"); fseek (fp , 0 , SEEK\_END); lSize = ftell (fp); rewind (fp); Newpasswd= (unsigned char\*) malloc (sizeof(unsigned char)\*lSize); int i = fread(Newpasswd,1,sizeof(Newpasswd),fp);//Newpasswd is not the same as encodedpasswd char\* tempPassword = DecodeString(testpasswd,&dlen);// here tempPassword is junk
Is writing is not done properly? Regards,
I guess the problem is because you are using sizeof to calculate the lenght of your string. Sizeof will return the size (in bytes) of the pointer, not what it contains. So replace the first sizeof by a strlen (if the string is NULL terminated). The others sizeof (in the malloc and in the fread) should be replaced by the size of the data that is retrieved from the file.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ -
Hi, How to write a BYTE array into a file and reading it back from. I used fwrite and fread but it is not working. Regards,
subramanyeswari wrote:
I used fwrite and fread but it is not working.
(Un)fortunately It doesn't depend on
fwrite
andfread
, my friend. The following works for me (error checking left as exercise)int main()
{
{
BYTE bValue[]={2,4,8};
// Length of the array
int iLen = sizeof(bValue)/sizeof(bValue[0]);
// WRITE array
// open file for output in binary mode
FILE * fpo = fopen("foo.bin", "wb");
// store array length
fwrite(&iLen, sizeof(iLen), 1,fpo);
// store array elements
fwrite(bValue, sizeof(bValue), 1, fpo);
fclose(fpo);
}{
BYTE * bValue;
// READ array
// open file for input in binary mode
FILE * fpi = fopen("foo.bin", "rb");
int iLen;
// read array length
fread(&iLen, sizeof(iLen), 1,fpi);
// read array elements
bValue = new BYTE[iLen];
fread(bValue, sizeof(BYTE), iLen, fpi);
fclose(fpi);
// use values
// ...
//Cleanup
delete [] bValue;
}
return 0;
}:)
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]