Thread: Reading from file into array, some strings aren't terminated?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    33

    Reading from file into array, some strings aren't terminated?

    I'm making a Jeopardy game and when I read text from a file into arrays to set the categories, questions and answers a couple of the strings are not terminating and they are printing that junk box thing at the end. When I use the debugger and print the problem strings they are showing up as "Thanksgiving\021" or "Calvin Klein\021" but all the other strings aren't having that problem. Then for example I will change "line[strlen(line)-1] = '\0';" to "line[strlen(line)] = '\0';" and other strings will have that problem but not those stated ones. I'm using line[strlen(line) - 1] = '\0' to get rid of the newline fgets appends on the end of the strings. Thanks for the help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[]){
       
       FILE *file = fopen("questions", "r");
       char *string[66], line[150];
       int idx;
    
    
       for(idx = 0; idx < 66; idx++){
          fgets(line, 150, file);
          line[strlen(line)-1] = '\0';
          string[idx] = (char *) malloc(strlen(line));
          strcpy(string[idx], line);
       }
    
    
       for(idx = 0; idx < 66; idx++){
        
          printf("%s\n", string[idx]);
       }
    
    
    
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    33
    Nevermind, I just figured it out! I wasn't terminating the strings in the array itself.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[]){
       
       FILE *file = fopen("questions", "r");
       char *string[66], line[150];
       int idx;
    
    
       for(idx = 0; idx < 66; idx++){
          fgets(line, 150, file);
          line[strlen(line)] = '\0';
          string[idx] = (char *) malloc(strlen(line));
          strcpy(string[idx], line);
          string[idx][strlen(string[idx]) - 1] = '\0';
       }
    
    
       for(idx = 0; idx < 66; idx++){
        
          printf("%s\n", string[idx]);
       }
    
    
       return 0;
    }

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I couldn't recreate your problem changing your input to stdin

    Something I picked up of was that you have a memory leak -> You are never freeing any memory you alloc.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, you are checking for no errors.

    You should check to see if file is not equal to NULL before using it
    You should check to see if malloc did not return NULL before using it
    You should not cast what malloc returns (It's unneccesary and considered bad practice in C, because it will hide an error from your compiler)
    Fact - Beethoven wrote his first symphony in C

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > string[idx] = (char *) malloc(strlen(line));
    You don't allocate space for the \0

    So whatever \0 you put there either trashes something else, or in turn is lost later on.

    string[idx] = malloc(strlen(line)+1);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Salem - I missed that because I was getting the input from stdin and the '\n' charactor was being replaced by the NUL character *face palm*
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    33
    Quote Originally Posted by Click_here View Post
    Also, you are checking for no errors.

    You should check to see if file is not equal to NULL before using it
    You should check to see if malloc did not return NULL before using it
    You should not cast what malloc returns (It's unneccesary and considered bad practice in C, because it will hide an error from your compiler)
    That code I posted was just a quick example, in my actual program I checked for a NULL file and used free() for the dynamic memory alloc. But I wasn't aware casting malloc was bad practice, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble Reading File of Strings into (Three-Dimensional) Array
    By JonSchaffer57 in forum C Programming
    Replies: 4
    Last Post: 04-12-2013, 08:37 AM
  2. Replies: 13
    Last Post: 04-12-2012, 06:09 AM
  3. Reading an array of strings from a text file?
    By jeanermand in forum C Programming
    Replies: 9
    Last Post: 11-14-2011, 09:41 PM
  4. Problem reading file into array of strings
    By myrddinb in forum C Programming
    Replies: 7
    Last Post: 03-26-2005, 12:12 AM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM