When you write datas in files, if you want to read it with a text editor, you need to convert variables to strings (serialization).
To do this, you can use fprintf
and fscanf
, or implement your own parsing functions, you only need to manipulate strings.
In order to generate a readable file, your code could be modified in:
FILE *f = fopen(argv[1], "w"); // "a", "w" or what you want
const int size = 20;
int *p = new int[size];
for(int i=0; i<size; i++) {
p[i] = i;
fprintf(f, "%d ", p[i]);
}
fclose(f);
To read the file, you could do:
FILE *f = fopen(argv[1], "r");
int nb = 0;
int tab[100]; // a buffer
while((1 == fscanf("%d ", &tab[nb])) && (nb < 100))
nb++;