首页 > 解决方案 > 在 Ubuntu 环境中使用 C 从 .txt 文件中的文本输出 CRC32 hex

问题描述

#include <stdio.h>                                                                                                                                                                                     
#include <stdlib.h>                                                                                                                                                                                       
#include <string.h>                                                                                                                                                                                       
#include <zlib.h>                                                                                                                                                                                                                                                                                                                                                                                                   
#define LSIZ 128                                                                                                                                                                                          
#define RSIZ 10                                                                                                                                                                                                                                                                                                                                                                                                     
int main()                                                                                                                                                                                                
{                                                                                                                                                                                                                 
    // gets input from input.txt                                                                                                                                                                              
    char *filename = "input.txt";                                                                                                                                                                             
    FILE *fptr;                                                                                                                                                                                               
    fptr = fopen(filename, "r");                                                                                                                                                                                                                                                                                                                                                                                        
    int i = 0, j, tot = 0;                                                                                                                                                                                    
    char line[RSIZ][LSIZ];
                                                                                                                                                                                                                                                                                                                                                                                              
    // inputs text content into array                                                                                                                                                                         
    while(fgets(line[i], LSIZ, fptr) != NULL)                                                                                                                                                                 
    {                                                                                                                                                                                                                 
        line[i][strlen(line[i]) - 1] = '\0';                                                                                                                                                                      
        i++;                                                                                                                                                                                              
    }                                                                                                                                                                                                         
    printf("\n");
                                                                                                                                                                                             
    tot = i;                                                                                                                                                                                                  
    printf("\nThe content of the file %s are: \n",filename);                                                                                                                                                  
    for(j = 0; j < tot; ++j)                                                                                                                                                                                          
        printf(" %s\n", line[i]);                                                                                                                                                                                                                                                                                                                                                                                   

    //convert into hex of crc32                                                                                                                                                                               
    const char *s = line[i];                                                                                                                                                                                  
    printf("%s's crc32 in hex: ",filename);                                                                                                                                                                   
    printf("%lX\n", crc32(0, (const void*)s, strlen(s)));                                                                                                                                                                                                                                                                                                                                                               
    return 0;                                                                                                                                                                                         
    } 

我似乎无法让代码按预期工作。我正在使用 vim,同时也在 Ubuntu 终端上编译此代码。我希望它从 input.txt 中获取文本,将其存储到数组中并创建所述输入的 crc32 hex。但是,此输出是结果:

The content of the file input.txt are:

input.txt's crc32 in hex: 0

char 输入肯定有问题,但浏览后,我似乎遇到了困难。任何帮助,将不胜感激!

标签: ciocrccrc32

解决方案


我编辑了您的问题,删除了您添加的答案,并在此处发布了该答案。

我的代码现在运行完美。我将在这里写下整个内容以供其他人参考:

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

#define LSIZ 128
#define RSIZ 10

int main(int argc, const char * argv[])
{
        // gets input from input.txt
        char *filename = "input.txt";
        FILE *fptr;
        fptr = fopen(filename, "r");

        int i = 0, j, tot = 0;
        char line[RSIZ][LSIZ];

        // inputs text content into array
        while(fgets(line[i], LSIZ, fptr) != NULL)
        {
                line[i][strlen(line[i]) - 1] = '\0';
                i++;
        }
        tot = i;
        printf("The content of the file %s are: ",filename);
        for(j = 0; j < tot; ++j)
                printf("%s\n", line[j]);

        //convert into hex of crc32
        const char *s = line[0];

        printf("%s's crc32 in hex: ",filename);
        printf("%lX\n", crc32(0, (const void*)s, strlen(s)));

        fclose(fptr);
        return 0;
}

这是 input.txt 文件中带有“Hello World”的输出:

The content of the file input.txt are: Hello World
input.txt's crc32 in hex: 4A17B156

推荐阅读