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. Problem in fwrite and fread function [modified]

Problem in fwrite and fread function [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
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.
  • M Offline
    M Offline
    mohindar_kks
    wrote on last edited by
    #1

    Hi, In my project simply encrypt data using XOR operator ,Then write encrypted data in file using fwrite function in MFC. Problem is occure only possible to decrypt pice of data from encrypted file. For example , File1:Input File size(6kb) FIle2:Encrypt File size(5kb) File3:Decrypt File size(1kb) coding: Encrypt: while(!feof(file1)) { int iout; fread(&iout,sizeof(iout),1,file1); iout^=1300; fwrite(&iout,sizeof(iout),1,file2); } Decrypt: while(!feof(file2)) { int iout; fread(&iout,sizeof(iout),1,file2); iout^=1300; fwrite(&iout,sizeof(iout),1,file3); } Note : NoProblem occur when if XOR with value of 10 while(!feof(file1)) { int iout; fread(&iout,sizeof(iout),1,file1); iout^=10; fwrite(&iout,sizeof(iout),1,file2); } Please replay me urgently -- modified at 8:40 Thursday 26th April, 2007

    D 1 Reply Last reply
    0
    • M mohindar_kks

      Hi, In my project simply encrypt data using XOR operator ,Then write encrypted data in file using fwrite function in MFC. Problem is occure only possible to decrypt pice of data from encrypted file. For example , File1:Input File size(6kb) FIle2:Encrypt File size(5kb) File3:Decrypt File size(1kb) coding: Encrypt: while(!feof(file1)) { int iout; fread(&iout,sizeof(iout),1,file1); iout^=1300; fwrite(&iout,sizeof(iout),1,file2); } Decrypt: while(!feof(file2)) { int iout; fread(&iout,sizeof(iout),1,file2); iout^=1300; fwrite(&iout,sizeof(iout),1,file3); } Note : NoProblem occur when if XOR with value of 10 while(!feof(file1)) { int iout; fread(&iout,sizeof(iout),1,file1); iout^=10; fwrite(&iout,sizeof(iout),1,file2); } Please replay me urgently -- modified at 8:40 Thursday 26th April, 2007

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      mohindar_kks wrote:

      Problem is occure only possible to decrypt pice of data from encrypted file.

      This is a bit unclear. What exactly is the problem? Are you concerned that file1 (the original) and file3 (the decrypted) are not the same? You'll likely see the same problem even if you omitted the XOR statement. Try:

      while (! feof(file1))
      {
      char iout;
      if (fread(&iout, sizeof(iout), 1, file1) == 1)
      {
      iout ^= 1300;
      fwrite(&iout, sizeof(iout), 1, file2);
      }
      }


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      M M 2 Replies Last reply
      0
      • D David Crow

        mohindar_kks wrote:

        Problem is occure only possible to decrypt pice of data from encrypted file.

        This is a bit unclear. What exactly is the problem? Are you concerned that file1 (the original) and file3 (the decrypted) are not the same? You'll likely see the same problem even if you omitted the XOR statement. Try:

        while (! feof(file1))
        {
        char iout;
        if (fread(&iout, sizeof(iout), 1, file1) == 1)
        {
        iout ^= 1300;
        fwrite(&iout, sizeof(iout), 1, file2);
        }
        }


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        M Offline
        M Offline
        mohindar_kks
        wrote on last edited by
        #3

        Hi, In my project simply encrypt data using XOR operator ,Then write encrypted data in file using fwrite function in MFC. No problem in Decryption if successfully retrive data from encrypted file until EOF. But some time encrypted file automatically return EOF after postion of data retrive this data also decrypted successfully. For example , File1:Input File size(6kb) FIle2:Encrypt File size(5kb) File3:Decrypt File size(1kb) coding: Encrypt: while(!feof(Inputfile)) { int iout; fread(&iout,sizeof(iout),1,Inputfile); iout^=1300; fwrite(&iout,sizeof(iout),1,encryptfile); } Decrypt: while(!feof(encryptfile)) { int iout; fread(&iout,sizeof(iout),1,encryptfile); iout^=1300; fwrite(&iout,sizeof(iout),1,decryptfile); } Note : NoProblem occur when if XOR with value of 10 while(!feof(Inputfile)) { int iout; fread(&iout,sizeof(iout),1,Inputfile); iout^=10; fwrite(&iout,sizeof(iout),1,encryptfile); } Please replay me urgently

        1 Reply Last reply
        0
        • D David Crow

          mohindar_kks wrote:

          Problem is occure only possible to decrypt pice of data from encrypted file.

          This is a bit unclear. What exactly is the problem? Are you concerned that file1 (the original) and file3 (the decrypted) are not the same? You'll likely see the same problem even if you omitted the XOR statement. Try:

          while (! feof(file1))
          {
          char iout;
          if (fread(&iout, sizeof(iout), 1, file1) == 1)
          {
          iout ^= 1300;
          fwrite(&iout, sizeof(iout), 1, file2);
          }
          }


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          char iout; ... iout ^= 1300; :~ :)

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          D 1 Reply Last reply
          0
          • M Mark Salsbery

            char iout; ... iout ^= 1300; :~ :)

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Bad, yes. Part of the problem, no. His two major problems were not checking the return value from fread(), and trying to read bytes from the disk as ints.


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            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