I am working on a project that reads input from a file ,stores them into an array and remove the spaces in each string array.
I am doing this so that I can check if the string that were read in the input file are correct. Then I will check if the array "cardarray" contains all the cards from stringcards. But I haven't gotten to that yet. Do you also have any tips how I could do that?
This what I have come up with but I keep getting an error that I am smashing the stack.
Code:
int main (int argc, char **argv) {
const char * stringcard[] = { "REDA","RED2"
"RED3"
"RED4"
"RED5"
"RED6"
"RED7"
"RED8"
"RED9"
"RED10"
"REDJ"
"REDQ"
"REDK"
};
char *reds[max];
char * cardarray[max];
int i;
FILE *file = argc > 1 ? fopen (argv[1], "r") : stdin;
if (file == NULL)
return 1;
if(argc!=2){
printf("[ERR]");
return 0;
}
for (i =0; i < max; i++) {
reds[i] = malloc( stringlength);
fgets(reds[i], stringlength, file);
}
int i2 = 0;
for (i =0; i < max; i++){
printf ("%s", reds[i]);
}
for(i= 0;i<max;i++){
char *p = strtok (reds[i], " ");
while (p != NULL)
{
cardarray[i2++] = p;
p = strtok (NULL, " ");
}
}
for (i =0; i < max; i++){
printf ("%s", cardarray[i]);
}
return 0;
}
Input:
Code:
RED A
RED 2
RED 3
RED 4
RED 5
RED 6
RED 7
RED 8
RED 9
RED 10
RED J
RED Q
RED K