0

For background, I'm currently working on an assignment to scan Les Miserables (given to me in a .txt file) for character names (a list of which is given to me in a separate .txt file). When a character is mentioned, write which character and which line they are mentioned in into a .txt file. My code currently records the character names into namesArr[66][20], and a 4-line excerpt from Les Miserables (a different file then the entire novel) into fourlinesArr[4][100].

Now to the actual problem. When attempting to check if a line of namesArr is in fourlinesArr, strstr will always return false. If I replace namesArr with a single character array, it works just fine however. Is there any way to get strstr to work properly with multidimensional arrays, or should I use a different function entirely?

Entire code below:

//import statements
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {

//Read for Les-Mis-Names.txt
//copy and pasted fileIO.c, editing where required
const char *NAMES_FILE_PATH = "./Moodle Documents/Les-Mis-Names.txt"; // location of names file
int nameslineNum = 0;
char namesArr[66][20];

FILE *names = fopen(NAMES_FILE_PATH,  "r+" );       /* open for reading */
// This will take each row in the file and store it in namesArr.
if (names == NULL ){      /* check does names file exist etc */
    perror ("Error opening names file");
    nameslineNum = -1; /* use this as a file not found code */
} else {
    // fgets returns NULL when it gets to the end of the file
    while ( fgets(namesArr[nameslineNum], sizeof(namesArr[nameslineNum]), names ) != NULL ) {
        nameslineNum++;
    }
    fclose (names);
}
//end of fileIO.c code 

//Read for Les-Mis-4-lines
//copy and pasted fileIO.c, editing where required
const char *LES_MIS_4_LINES_FILE_PATH = "./Moodle Documents/Les-Mis-4-lines"; // location of Les-Mis-4-lines file
int fourlineslineNum = 0;
char fourlinesArr[4][100];

FILE *fourlines = fopen(LES_MIS_4_LINES_FILE_PATH,  "r+" );       /* open for reading */
// This will take each row in the file and store it in fourlinesArr.
if (fourlines == NULL ){      /* check does 4 lines file exist etc */
    perror ("Error opening 4 lines file");
    fourlineslineNum = -1; /* use this as a file not found code */
} else {
    // fgets returns NULL when it gets to the end of the file
    while ( fgets(fourlinesArr[fourlineslineNum], sizeof(fourlinesArr[fourlineslineNum]), fourlines ) != NULL ) {

        fourlineslineNum++;
    }
    fclose (fourlines);
}
//end of fileIO.c code


//testing
char name[20] = "Fabantou";
printf("%s\n", namesArr[62]);
printf("%s\n", fourlinesArr[0]);

if(strstr(fourlinesArr[0], namesArr[62]) != NULL) {
    printf("Success\n");
}
else {
    printf("Failure\n");
}

if(strstr(fourlinesArr[0], name) != NULL) {
    printf("Success\n");
}
else {
    printf("Failure\n");
}

return 0;
}

Which returns:

Fabantou

"Madame Fabantou seems to me to be better," went on M. Leblanc, casting

Failure

Success
Pooya
  • 6,083
  • 3
  • 23
  • 43
DJOwen
  • 21
  • 1
  • 7
  • 1
    The problem is that `fgets` puts the newline character into the buffer. [Here are suggestions](http://stackoverflow.com/a/28462221/3386109) on how to remove the newline. – user3386109 Mar 16 '16 at 21:43
  • 1
    The hidden characters strike again, that suggestion seems to have worked. Thanks for your help! – DJOwen Mar 16 '16 at 22:02

0 Answers0