I'm trying to loop through a text file (seven lines in the file, with aaaa, aaab, aaac, etc listed) with password guesses for pset2 Crack. I am able to open the file, and iterate through the lines. However, my output is not what I expect at all. Here is the relevant code:

FILE* list = fopen("test.txt", "r");
char line[7]; //amount of lines in list.txt 456975

// iterate through each line of the file
while (fgets(line, sizeof(line), list))
    char nline[3] = ""; //initialize "new line" that will be used to hold string

    sscanf(line, "%s", nline); //convert the contents of "line" into a string and read it
    string guess = strcat(salt, nline); // catenate the salt to the front of the guess
    printf("%s\n", guess);

printf("%s\n", hash);

and the output that I get is this:

Segmentation fault

I've tried using the debugger in the ide, but I can't figure that out (if you can point me to a good tutorial on that, I'd appreciate it).

I'm really just looking for where to start debugging here. I sat with this for 2 hours last night and can't make any headway. I know it has something to do with not looping correctly, as I should only see "aaaa" once, but it seems to be adding on the new line and printing a catenation of that instead of just a single guess per line. I expected to see:


The segmentation fault is throwing me for a loop (no pun intended) as well. I don't mind pouring over documentation and I'm not looking for a quick fix here, I just feel like I'm spinning and need to pointed in the right direction. Thanks in advance!

  • 1
    Put +1 as the quality of the question is very good. However, it looks like for me as the russian (i'm russian) proverb: "Shot a sparrow with a connon". I'm sure there is an analog in English, I just don't know it. However, the sense is quite clear: to solve nothing just a simple task you use sophisticated powerful tools witch consumes a lot of resources.
    – obesman
    Jul 14, 2017 at 12:02
  • I'm not American (I'm from Greece) but I think I have read the phrase "swatting flies with a sledgehammer". But with one word I think it's called "Overkill".
    – ChrisG
    Jul 15, 2017 at 9:00
  • What is fgets()?
    – fwoosh
    Mar 26, 2019 at 3:44
  • Also, have you watched the lecture.
    – fwoosh
    Mar 29, 2019 at 2:53

1 Answer 1


Let's take a look at the man page of strcat():

char *strcat(char *dest, const char *src);

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and adds a terminating null byte.

So, it seems that strcat() doesn't just return to you a new string with the result of the concatenation, but it concatenates the two strings, and puts the result into the first string (dest)!

So when you concatenate again later, the dest string is the result of the previous concatenation, plus the new concatenation, and that's why you end up with this railroad of string, and get the segfault later.

You should instead do something like the following:

#include <stdio.h>
#include <string.h>

#define MAX_LEN 5

int main(void)
    FILE *infile = fopen("test.txt", "r");
    char line[MAX_LEN + 2];   // each line in test.txt is MAX_LEN chars long, +1 for the '\n', +1 for the '\0'
    char salt[] = "50";

    char result[8]; // 2 for the salt, +5 for the password, +1 for the '\0'

    while(fgets(line, MAX_LEN + 2, infile))
        line[MAX_LEN] = '\0';   // replace the '\n' from the end of the line, with '\0'
        strcpy(result, salt);
        strcat(result, line);
        printf("%s\n", result);

which prints:

  • Just curious, what is the purpose of defining MAX_LEN? Also, what does setting line[MAX_LEN] to "\0" do? Jul 15, 2017 at 2:46
  • MAX_LEN is the maximum width of all words in dictionary. I have defined it in the beginning in order to avoid so called "magic numbers" in code. Magic numbers are constants that appear in your code but are not explained. It's also easier to change the definition at the top if something changes, instead of changing the value at every point in your code. As for your second question, because fgets(), according to its documentation, also reads the new line at the end of the line, and appends it to the string, I replace the '\n' with a '\0' to end the string there.
    – ChrisG
    Jul 15, 2017 at 8:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .