c - 在 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 输入肯定有问题,但浏览后,我似乎遇到了困难。任何帮助,将不胜感激!
解决方案
我编辑了您的问题,删除了您添加的答案,并在此处发布了该答案。
我的代码现在运行完美。我将在这里写下整个内容以供其他人参考:
#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
推荐阅读
- swift - 如何从 AnyHashable 类型的字典中获取数据?
- linux - 将 CSV 文件中的第一行和第三行转换为 shell 脚本中的列
- java - 在详细信息表单上进行用户导航的最佳入门
- java - 来自 SD 卡的图像未加载到 WebView 中
- python - 使用循环计算数据点
- python - “if”条件语句上的数据类型错误
- c# - 如何使用正则表达式从列表中提取字符串匹配?
- c# - 为什么 WebExceptionStatus.Timeout 没有捕获到超时错误?
- kubernetes - 如何在 Kafka 代理上添加 HAproxy
- json - 如何从其网络面板下载此页面评论的 JSON 文件?