Thread: Issues with writing data to binary files

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    13

    Issues with writing data to binary files

    Im trying to write my custom file encrypter, but the outfile fp2 is empty so I'm doing something wrong here:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #defineSIZE(a) sizeof(a) / sizeof(a[0])
    
    void encrypt(const char *fileName, const char *newFileName, const char *KWORD,
                 int K)
    {
      FILE *fp, *fp2;
      fp = fopen(fileName, "rb");
      fp2 = fopen(newFileName, "a+");
      if (!fp) {
        printf("\nError: Could not open the file %s\n", fileName);
      } else {
        printf("\nFile has been opened successfully\n");
    
        int block[8192], xorblock[8192];
        char banner[128];
    
        size_t allBytes, byteCount =
            fread(banner, sizeof(banner), sizeof(banner), fp);
        printf("BASE FILE BANNER: %s\n", banner);
        fseek(fp, 0, SEEK_SET);
    
        while (byteCount) {
          byteCount = fread(block, sizeof(block), sizeof(block), fp);
          allBytes += byteCount;
          printf("\rByte has been read: %zu\r", byteCount);
    
          for (size_t i = 0; i < SIZE(block); i++) {
            K = K % strlen(KWORD);
            volatile char kwordPos = KWORD[K];
            xorblock = block ^ (int) kwordPos;
            K++;
          }
          fwrite(xorblock, sizeof(xorblock), sizeof(xorblock), fp2);
        }
        printf("\nAll bytes read:%zu\n", allBytes);
    
      }
      fclose(fp);
      fclose(fp2);
    
      FILE *fp2read;
      fp2read = fopen(newFileName, "rb");
      char newBanner[128];
      fread(newBanner, sizeof(newBanner), sizeof(newBanner), fp2read);
      printf("\nNEW FILE BANNER: %s\n", newBanner);
    }
    
    int main()
    {
      const char *myFileName = "Example.txt";
      char newFileName[17] = "_NEW_";
      const char *keyWord = "Keyword";
      int Key = 'K';
    
      strcat(newFileName, myFileName);
      encrypt(myFileName, newFileName, keyWord, Key);
      printf("%s%zu\n", newFileName, strlen(newFileName));
      printf("\nAll done.Quitting now.\n");
      return 0;
    }
    fsanitizer is showing that something is crashing pretty badly:
    File has been opened successfullyBASE FILE BANNER: C plus plus


    All bytes read:0
    AddressSanitizer:DEADLYSIGNAL
    ================================================== ===============
    ==571==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f2229726005 bp 0x7ffe1b18abe0 sp 0x7ffe1b18a320 T0)
    ==571==The signal is caused by a READ memory access.
    ==571==Hint: address points to the zero page.
    AddressSanitizer:DEADLYSIGNAL
    AddressSanitizer: nested bug in the same thread, aborting.
    Last edited by Salem; 11-19-2021 at 09:37 PM. Reason: Removed crayola - AGAIN - Learn how to paste code without the mickey mouse

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I writing binary data correctly
    By generaltso78 in forum C Programming
    Replies: 13
    Last Post: 03-06-2013, 03:14 PM
  2. Replies: 3
    Last Post: 09-30-2008, 12:10 AM
  3. Writing binary data to a file
    By zacs7 in forum C Programming
    Replies: 5
    Last Post: 10-24-2007, 04:00 PM
  4. Help with Reading and writing binary data
    By Yasir_Malik in forum C Programming
    Replies: 3
    Last Post: 12-12-2004, 09:24 AM
  5. writing to binary files....
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2003, 09:58 PM

Tags for this Thread