Thread: Read from a Text File - Segmentation Fault

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    2

    Read from a Text File - Segmentation Fault

    Hello All,

    This post is regarding reading from a text file.

    Background - I am trying to fill my Text struct field member char** lines with the strings from text file. However, I am having trouble simply reading from the text file itself.

    I am thinking that there is an error with my while loop but am having some difficulty debugging. Anyone have an idea why my while loop is triggering a Segmentation Fault?

    My code is here:

    Code:
    bool fill_text(Text* text, char* fileName)
    {
          FILE *filePtr = fopen(fileName, "r");
          
          if(filePtr == NULL)
          {
                return false;
          } else {
    
                char** line = text->lines;
                int max = text->maxLineCount;
                int iter = 0;
    
                while (fgets(line[iter], max, filePtr) != NULL)
                {
                      // Printing line[iter] here to see if fgets and while are working correctly.
                      printf("%s\n",line[iter]);
                      iter++;
                }
    
                return true;
          }
    
    }
    Last edited by michaelaah; 10-12-2020 at 07:24 AM. Reason: code spelling error update

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just in case, allow me to ask the obvious question: did you allocate sufficient storage for the strings? I notice that you have max line count, but that sounds like the maximum number of lines rather than the maximum number of characters (including null character) per line, which is what fgets requires. You should be comparing iter with max instead to avoid exceeding the max number of lines.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    2
    aah, good point! I was not certain I had to allocate storage for the strings. (I am a bit new to pointers and memory allocation in C). The trick with this problem is that the size of the text file to be read from is unknown - meaning the number of lines, characters, and number of strings is unknown as well. Perhaps I could allocate a large amount of memory before hand?

    Your understanding about what max line represents is correct.

    I thought that fgets would read from the text file until the EOF character was encountered - or is that assumption incorrect?

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    It depends when fgets stops.
    Let's say you want to read 80 characters. If the file is less than 80 charaters long, fgets will stop when reaching the end.
    If fgets reads an '\n' before 80 characters are read it will stop too, otherwise it will stop after 80 characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault when I try to read in the lines of a file
    By Zsofia Komaromi in forum C Programming
    Replies: 1
    Last Post: 10-21-2013, 02:13 PM
  2. Reading from file, segmentation fault
    By atac in forum C Programming
    Replies: 2
    Last Post: 05-08-2013, 10:34 PM
  3. Segmentation fault in Header file
    By Marc Oleson in forum C Programming
    Replies: 12
    Last Post: 04-26-2013, 05:39 PM
  4. Segmentation fault reading and parsing data from a text file
    By deathseeker25 in forum C Programming
    Replies: 4
    Last Post: 05-19-2012, 12:33 PM
  5. Segmentation fault when reading very large text files
    By sapogo06 in forum C Programming
    Replies: 8
    Last Post: 12-07-2009, 03:19 PM

Tags for this Thread