почему один l oop работает, но практически идентичный l oop не печатает данные в файл? - PullRequest
0 голосов
/ 20 апреля 2020

Этот код ничего не печатает в hashfile2.txt, но печатает в hashfile1.txt, и я совершенно не понимаю, почему. Если я переместу l oop, который управляет hashfile2.txt, до hashfile1.txt, он также не будет работать. Если я не пропускаю что-то полностью, они оба являются одинаковыми циклами с разными переменными одного типа.

void readfiles()
{
    FILE *file1 = fopen ("plaintext1.txt", "r");
    FILE *file2 = fopen ("plaintext2.txt", "r");
    char results[] = "results.txt";
    FILE *resultfile = fopen(results, "w");
    FILE *hash1 = fopen("hashfile1.txt", "w");
    FILE *hash2 = fopen("hashfile2.txt", "w");
    char hash1line[128];
    char hash2line[128];
    char plaintext1line[128];
    char plaintext2line[128];
    unsigned char sha256_digest[SHA256_DIGEST_LENGTH];
    unsigned char md5_digest[MD5_DIGEST_LENGTH];
    unsigned char newline[] = {" "};

    if(file1 != NULL && file2 != NULL)
    {
        while (1)
            {
                if (fgets(plaintext1line, sizeof plaintext1line, file1)!= NULL && fgets(plaintext2line,
             sizeof plaintext2line, file2)!= NULL)
                {
                        location.file1read = (unsigned char *)plaintext1line;
                        location.file2read = (unsigned char *)plaintext2line;
                        if (location.algorithm == 0)
                        {
                            MD5(location.file1read, sizeof location.file1read, md5_digest);

                            for (int i = 0; i < sizeof md5_digest; i++)
                            {
                                fprintf(hash1, "%x", md5_digest[i]);
                            }
                            fprintf(hash1, "%s\n", newline);

                            MD5(location.file2read, sizeof location.file2read, md5_digest);

                            for (int j = 0; j < sizeof md5_digest; j++)
                            {
                                fprintf(hash2, "%x", md5_digest[j]);
                            }
                            fprintf(hash2, "%s\n", newline);
                        }

                        else if (location.algorithm == 1)
                        {
                            SHA256(location.file1read, sizeof location.file1read, sha256_digest);
                            for (int i = 0; i < sizeof location.file1read; i++)
                            {
                                fprintf(hash1, "%x", sha256_digest[i]);
                            }
                            fprintf(hash1, "%s\n", newline);
                            SHA256(location.file2read, sizeof location.file2read, sha256_digest);

                            for (int i = 0; i < sizeof location.file2read; i++)
                            {
                                fprintf(hash2, "%x", sha256_digest[i]);
                            }
                            fprintf(hash2, "%s\n", newline);
                        }

                    for(int i = 0; fgets(hash1line, sizeof hash1line, hash1) !=NULL; i++)
                    {
                        for (int j = 0; fgets(hash2line, sizeof hash2line, hash2) !=NULL; j++)
                        {
                            if(strcmp(hash1line, hash2line) == 0)
                            {
                                fprintf(resultfile, "collision found on line %d of %s %s\ncollision found on %d of %s %s\n", 
                                i, location.hashfile1, hash1line, j, location.hashfile2, hash2line);
                            }
                        }
                    }
                }
            else 
            {
                   break;
            }
            }
        }
  }     
...