0

everyone! I really need your help. I'm working on pset4 and trying to understand the logic of resize problem.

I have two files:testinp.txt and testout.txt. Inside of testinp.txt I have:

1111000011110000 0000111100001111

What I want to do is to read the contents of testinp.txt and write them to testout.txt using approximately the same method that is used in resize to iterate over scanlines in infile and write pixels to outfile. Here's my code:

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

int main(void) {

int width = 16;
int height = 2;

FILE* inptr = fopen("testinp.txt", "r");
char arr[sizeof(char)];
FILE* outptr = fopen("testout.txt", "w");

//iterate over scanlines
for (int i = 0; i < height; i++) {

  //iterate over pixels in scanline
  for (int j = 0; j < width; j++) {
    fread(&arr, sizeof(char), 1, inptr);
    fwrite(&arr, sizeof(char), 1, outptr);
  }
}

fclose(inptr);
fclose(outptr);

}

For some reason it doesn't write all the characters to testout.txt. I just get: 1111000011110000 000011110000111 As you can see the last 1 is missing and I don't understand why. What am I doing wrong?

1

There is "\n" (new line) char in the end of the first raw. So there are 33 chars, while you read-write only 32.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .