Question About Reading Files
Hello,
I'm trying to take files as command line arguments that have one string on each line. Then, I want to put each line into an array of strings. Here is my code:
Code:
#include <stdio.h>
#include <string.h>
main(int argc, char *argv[]) {
char buf[BUFSIZ];
char *sorted[BUFSIZ];
char *tok;
int stringNum;
int i;
FILE *fp;
if (argc > 10) {
fprintf(stderr, "%s: Too many command line arguments!\n",argv[0]);
exit(1);
}
fp = fopen(argv[1],"r");
while ( fgets(buf, BUFSIZ, fp) ) {
sorted[stringNum] = buf;
stringNum++;
}
printf("%s", sorted[0]);
}
I don't know what's wrong with this, but for some reason, it seems to be filling up the array with just the last line in the file. I'm not really familiar with opening and using files.