Don't do this:
Code:
char* a[2] = {"0123456789", "2"};
You declared a at file scope, making it a global variable, and then gave it a name that is not descriptive. Furthermore, you failed to use const, yet arranged for the pointers to point to string constants, risking undefined behaviour. I would suggest something like this:
Code:
int main(void) {
const char *const output[] = {"0123456789", "2"};
I declared what the pointers point to as const because they will point to string constants, then I declared the pointers themselves as const because you never modify this, and don't seem to need to. If you have more context as to what this output is about, it would be better to use that in the name instead of the more generic name of "output".
Next, as john.c noted in post #2, you wouldn't normally use fwrite to write, so I would change this:
Code:
fwrite(a[i], 1, strlen(a[i]) + 1, file_01);
to:
Code:
fprintf(file_01, "%s\n", output[i]);
If you really want to use fwrite, it can be done, but then you need to know the string length in advance (i.e., typically this means writing fields of equal length, e.g., by writing a fixed size array, or a struct containing a fixed size array), which is impossible with the file format that you came up with.
Having made these changes, we can move on to your read portion. You should be aware that this creates a variable length array:
Code:
int x0 = sizeof(a)/sizeof(a[0]);
char* b[x0];
The variable length array feature was standardised in the 1999 edition of the C standard, but later became optional, so you should not use it unless you are sure of your code being compiled by a compiler that supports that language feature. Yet, you don't need a variable length array here. You could have written:
Code:
char *input[sizeof(output) / sizeof(output[0])];
Now, input is a fixed size array. But... you have another problem: you don't have any space allocated for the strings themselves, and you cannot know how much space needs to be allocated without first reading from file, so it is a bit of a chicken and egg scenario.
For convenience, I suggest that you assume that there's a known upper bound to the length of the input strings, then arrange for arrays of char to be of that upper bound. For example, you could use BUFSIZ from <stdio.h>:
Code:
char input[sizeof(output) / sizeof(output[0])][BUFSIZ];
Having done that, you can now do what you planned:
Code:
while (fscanf(file_01, "%s", input[i2]) == 1) {
printf("%s\n", input[i2]);
i2++;
}
Note that this is technically vulnerable to buffer overflow: we should construct the format string such that the %s format specifier has a field width corresponding to the upper bound of the length of the input strings.