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. writing and reading BYTE array

writing and reading BYTE array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorial
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.
  • S Offline
    S Offline
    subramanyeswari
    wrote on last edited by
    #1

    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,

    C CPalliniC 2 Replies Last reply
    0
    • S subramanyeswari

      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,

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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++

      S 1 Reply Last reply
      0
      • C Cedric Moonen

        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++

        S Offline
        S Offline
        subramanyeswari
        wrote on last edited by
        #3

        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,

        C 1 Reply Last reply
        0
        • S subramanyeswari

          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,

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          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++

          1 Reply Last reply
          0
          • S subramanyeswari

            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,

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

            subramanyeswari wrote:

            I used fwrite and fread but it is not working.

            (Un)fortunately It doesn't depend on fwrite and fread, 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]

            In testa che avete, signor di Ceprano?

            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