-
so frustrated!!!!
i am scanning a input file and storing it to a string array.
char *station[][MAX]={0};
int scount=0;
do {
station[scount]=gets(sstr);
scount++;
} while (strcmp("####",sstr) != 0);
when i print out my array.. it is overwritten with "####"
for(i=0;i<scount;i++) {
printf("%s\n" station[i]);
}
this is my output.
####
####
####
####
####
####
####
####
i try to fix this by making my array to a 2x2 array.. but i can't get it to work correctly.. could someone help me??
-
Here's one way to do it.
Code:
#include <string.h>
char *station[MAX]={0};
int scount=0;
do {
gets(sstr);
station[scount] = malloc(strlen(sstr)+1);
strcpy(station[scount],sstr);
scount++;
} while (strcmp("####",sstr) != 0);
//when i print out my array.. it is overwritten with "####"
for(i=0;i<scount;i++) {
printf("%s\n",station[i]);
}
-
Or you can do it like this and leave out the line above with malloc().
char station[MAX][80]={0};
-
It helps if you actually provide all of the information we need to assist. Where is 'sstr' defined in your program? I see nothing eluding to it. We'll assume it's a file. What's in your file?
Quzah.