1

I am currently trying to create a binary byte patcher but got stuck at one little issue. When trying to read the input file byte by byte and simultanously writing these bytes to an output file, newline characters are somehow doubled.

An input like that:

  line1
  line2
  line3

Would look like:

  line1

  line2

  line3
   

The actual program is a bit more complex but this abstract should give an idea of what I'm trying to do.

  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>

  #define SECTOR 32

  int main(int argc, char** argv) {
    FILE* input = fopen(INPUT_FILE, "r");
    FILE * output = fopen(OUTPUT_FILE, "w");
    int filesize = size of opened file...
    char buffer[16] = {0};

    int i = 0;
    while(i < filesize) {
      fseek(input, i, SEEK_SET);
      fread(buffer, sizeof(char), SECTOR, input);

      if(cmp buffer with other char array) {
        do stuff

      } else {
        i++;
        fwrite(&buffer[0], sizeof(char), 1, output);
      }
    }
    print rest of buffer to the file to have the past SECTOR-1 bytes aswell...
    fclose(input);
    fclose(output);
    return 1;
  }
McSebi
  • 129
  • 2
  • 11
  • 3
    You're not opening files in binary mode (`"b"` in second argument to `fopen()`), so they probably aren't. Text mode does various magic to newlines. – unwind Apr 22 '16 at 10:24
  • Replace `fopen(INPUT_FILE, "r");` by `fopen(INPUT_FILE, "rb");` and `fopen(OUTPUT_FILE, "w");` by `fopen(OUTPUT_FILE, "wb");` – Jabberwocky Apr 22 '16 at 10:31
  • 3
    `char buffer[16] = {0};` --> `char buffer[SECTOR] = {0};` ` – BLUEPIXY Apr 22 '16 at 10:49
  • Thanks, opening the file in binary mode makes everything work as expected. I must've somehow missed that out while scrolling through pages of documentation. What BLUEPIXY spotted out just happened when i was extracting the relevant code from the src file :) Thanks anyway :) – McSebi Apr 23 '16 at 19:45

1 Answers1

0

Try to use open,close,read ans write instead of fopen fread fwrite and fclose. You should get something like that:

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFFSIZE 32

int main(int argc, char** argv) {
    int input = open(INPUT_FILE, O_RDONLY);
    int output = open(OUTPUT_FILE, O_WRONLY | O_CREATE, S_IWUSR | S_IRUSR);
    int i = 0;
    char buffer[BUFFSIZE];


    while((i = read(input, buffer, BUFFSIZE)))  {
        if(cmp buffer with other char array) {
            do stuff
        } else {
            while (i < 0) {
                i--;
                write(output, &buffer[i], sizeof(char));
            }
        }
    }
    close(input);
    close(output);
    return 1;
}
  • I'm using windows running mingw, the fcntl is propably where the functions are in, right? I'll give it a try. – McSebi Apr 22 '16 at 14:28