0

I want to write the content of an array into a file. The code below is a simplified program of mine. It does create the file "test.txt", but it only writes a bunch of 0 and \ into the file. What am I doing wrong?

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

int main(){

    int a[10],i;
    for(i=0;i<10;i++) a[i]=i;

    FILE* f = NULL;
    f = fopen("test.txt","ab+");
    fwrite(&a,sizeof(int),sizeof(a),f);

    return 0;
}

These are the contents of the file:

\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00‌​\00\00\00\00\00\00\0‌​0\00 \00\00\00\90@\0‌​0\00\00\00\00P8\00\0‌​0\00\00\00\00\00\00 \00\00\00\00\00\00\00\00\00\00\00\B9\C5?\00\00\00\00\00\00 \00\00\008%j\FC\00\00\00\00\00\00\00\00\00\80@\00\00\00\00\0‌​0\00\00\00\00\00\00\‌​00\00\AD\E6\FE\8A\F6‌​T\90@\00\00\00\00\00‌​0%j\FC\00\00\00\00\0‌​0\00\00\00\00\00\00\‌​00\00\00\00\00\00\00

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

1

You are failing to distinguish between values and representations. Understanding this difference is a vital skill every programmer needs, it's as important as understanding the distinction between things and stuff.

If you want to write the values of the array to a file, you'll have to convert them into a sensible representation to write to the file. Your code never does that.

Suppose I want to tell you how many cars I own. And it's two cars. I have to convert the number two into some form that I can communicate to you. Since we both speak English, I can say the word "two". But if I just tried to present to you the internal way my brain encodes the number of cars I have, you wouldn't be able to make any sense out of it. That encoding will only make sense to my brain.

Your code tries to write the internal encoding of the array to the file directly. But who knows how to make sense of that internal encoding? It will look like gibberish.

It's like trying to actually write the number three on a piece of paper. You can write a representation of it, like "three" or "3" or "III". But you can't just take the number itself and somehow write it on the paper. That's a category error.

Similarly, if you are happy and want to tell people that you are happy, you have to pick a language and a way to encode that language (spoken or written) and represent the idea that you are happy in that chosen language and form. You can't just output that fact that you happy without first choosing an appropriate representation and encoding the idea appropriately for transport to others. Again, that's a category error.

Punch "serialization" -- the process of converting internal encodings into sensible streams of bytes with a well-defined format -- into your favorite search engine.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • I think I understand my mistake. But how do I fix it? Do I need to convert it into a string or something like that? –  Nov 15 '17 at 08:28
  • @Rismosch You need to serialize it somehow. A file is a stream of bytes, so if you want to write something sensible to a file, you have to assemble a sensible chunk of bytes and write them to the file. Strings would be one way. – David Schwartz Nov 15 '17 at 09:21