C
C
Chemodan2282020-02-21 22:28:05
C++ / C#
Chemodan228, 2020-02-21 22:28:05

Error reading binary file?

#include <stdio.h>
#include <malloc.h>
#include "library.h"
int main() {
    FILE *in;
    queue q;
    queue_init(&q);
    in=fopen("input.bin","w");
    int *array,array_quantity,st=0,num;
    scanf("%i",&array_quantity);
    array=(int*)malloc(array_quantity* sizeof(int));
    for (int i=0;i<array_quantity;i++)
        scanf("%i",&array[i]);
    fwrite(array, sizeof(int),array_quantity,in);
    free(array);
    fclose(in);
    in=fopen("input.bin","r");
    while((num=fgetc(in))!=EOF){
      //  queue_push(&q,num);
        printf("%i ",num);
    }
    //while (!q.quantity){
      //  printf("%i",q.quantity);
    //}
    fclose(in);
    return 0;
}

The problem is that at the beginning I add the numbers 1 2 3 (example) and from the file I get 1000 2000 3000
What's the matter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-02-21
@TopToster

fwrite(array, sizeof(int),array_quantity,in);
...
    while((num=fgetc(in))!=EOF){
      //  queue_push(&q,num);
        printf("%i ",num);
    }

at the beginning I add the numbers 1 2 3 (example) and from the file I get 1000 2000 3000
What's the matter?

Doesn't it bother you that you write array_quantityelements of size to the file sizeof(int), and read from the file character by character?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question