I've run into a bit of a wall using fscanf to read from a text file. Please, no diatribes about the limitations of fscanf. For a known, invariant file structure it should work fine... Anyway.....
Lets declare:
char *filelist[40], *listfile; --where listfile is a text file with one filename per line.
FILE *fp;
And:
filelist[0] = "filename1.ext";
filelist[1] = "filename2.ext";
.
.
.
filelist[5] = "filename5.ext";
Then:
for(i = 0; i < 5; i++) printf("%s\n", filelist[i]);
This does what is expected and outputs a list of the filenames. However; if I do:
fp = fopen(listfile, "r");
for(i = 0; i < 5; i++)
fscanf(fp, "%s", filelist[i]);
And then do:
for(i = 0; i < 5; i++) printf("%s\n", filelist[i]);
It does not. What it outputs is this:
filefilefilefilefilename5.ext
filefilefilefilename5.ext
.
.
filename5.ext
It's obvious that what is happening is that filelist[i] is being incremented by machine int/32 bits/4 bytes rather than by the length of the previous string. Manually incrementing filelist by the length of the previous string doesn't solve the problem because when I go back to filelist[0]; it outputs garbage.
Does anybody know why it increments properly when I explicitly assign the strings but not when reading with fscanf? I have verified that fscanf is returning the full string + the \0 line feed character. I really hate scanning one character at a time when I shouldn't need to.