2

I've a problem with my .c program. I'll explain it :

  • I have 2 files (listePassword.txt and system_1.phl)
  • First file contains 10 passwords (like 123456, 12345678, admin, etc)
  • The second contains 10 hashs (f31041d6d9c2031086bfe561d8e4b63f or 77b6508f00223102d793837b9dd60358 for example)

I made a function to read lines on these files (1 word / hash by line). The code :

int main(int argc, char const *argv[]) {

   FILE *f = fopen("listePasswords.txt","r");
   FILE *S1 = fopen("system_1.phl", "r");

   unsigned char passwords[10][32]; 
   unsigned char system_1[10][32];

   lireFichier(f, passwords);
   lireFichier(S1, system_1);

And now the function :

void lireFichier(FILE *f, unsigned char (*resultats)[32]) {
    int i = 0;
    if(f){
        while(fgets(resultats[i], sizeof(resultats[i]), f) != 0) {
            printf("%s\n", resultats[i]);
            i++;
            fgetc(f);
        }
    }
}

The program doesn't read first char of each password except the first word, and I don't have all lines of the system_1.phl.

Can you help me a little ? :D (don't help with "{" or "}", I probably didn't paste them all).

Thanks guys ! (sorry, I'm French and not so good in English ahah).

AShelly
  • 34,686
  • 15
  • 91
  • 152

1 Answers1

2

The problem is the fgetc(f) in the loop. That's reading the first character of the next line.

I assume you intended it to read the newline at the end of the line. But fgets() includes that in its result, so you don't need to skip over it. Instead, you should remove it from resultsat[i]:

while (fgets(resultsat[i], sizeof resultat[i], f) != 0) {
    int len = strlen(resultat[i]);
    if (len > 0 && resultat[i][len-1] == '\n') {
        resultat[i][len-1] = '\0';
    }
    printf("%s\n", resultat[i]);
    i++;
}

You also need to change [32] in the array declarations to at least [34]. Your hashes are 32 characters long, but fgets() will read the following newline and also needs to add a null byte to terminate the string. So you need at least 2 more bytes in the arrays.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Your answer fix the problem with passwords (it's a mistake from me, i forget i try to use fgetc to skip the last char, but it's okay). But the problem is already here with hashes... When i use what you wrote, i've this result : lireFichier(S1, system_1); for (int i = 0; i < 10; ++i) { printf("Taille : %d + %s\n", strlen(system_1[i]), system_1[i]); } Taille : 31 + f31041d6d9c2031086bfe561d8e4b63 Taille : 1 + f Taille : 31 + 77b6508f00223102d793837b9dd6035 Taille : 1 + 8 In my file, i've : f31041d6d9c2031086bfe561d8e4b63f 77b6508f00223102d793837b9dd60358 what am I forget? –  May 10 '18 at 20:14
  • Your hashes are 32 characters long, so the arrays need to be `[33]` to allow room for the null byte. – Barmar May 10 '18 at 20:23
  • Yeah, it's really good now ! But I see an other problem with my file, so I open it with an Hex editor. The strings are : f31041d6d9c2031086bfe561d8e4b63f. 77b6508f00223102d793837b9dd60358. So, i need to read 34 char, and not 33. But with your help, it's fine now ! Thank you so much :D –  May 11 '18 at 11:14