gpt4 book ai didi

c - 读取文件然后将字符串与该文件中的内容进行比较的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-30 18:38:26 28 4
gpt4 key购买 nike

读取文件然后将字符串与该文件中的内容进行比较的最佳方法是什么?几天来我一直在努力解决这个问题,但我只是不知道该怎么做。我将不胜感激一些帮助。

最佳答案

您的问题概括地概括了从文件中读取字符串,并与另一个字符串进行字符串比较。要从您的文件中读取,您必须首先打开文件进行读取。对于从文件中读取文本,您在 C 中的正常选择是 fopen打开流并将返回分配给 FILE可与标准库中的流读取函数集合一起使用的指针。 (例如 fgetcfgetsfscanfgetline 等。)

你的工作是打开一个文件,验证它是否打开,从文件中读取所需的信息,然后关闭文件。为此,您需要准备一个具有足够空间的缓冲区(字符数组或分配的内存块)来保存从文件中读取的信息(+1 char 用于 nul 终止字符)。如果没有 nul 终止字符,则您有一个字符数组而不是有效字符串)一个简短的示例可能是:

char buf[256] = {0};  /* char array to hold 255 chars + nul-terminating char */
FILE *fp = NULL; /* FILE pointer to hold return of fopen */

fp = fopen (filename, "r"); /* open file for reading */
if (fp == NULL) { /* if not open throw error */
fprintf (stderr, "error: file open failed 'filename'.\n");
exit (EXIT_FAILURE);
}

如果您正在读取文件中没有包含空格的单个字符串,您可以简单地使用 fscanf 读取第一个单词。进入 buf . fscanf返回与给定格式说明符匹配的成功转换次数。将字段宽度选项与格式说明符一起使用,以确保您读取的内容不超过缓冲区将容纳的内容(例如 "%255s" ),为最后的 nul 终止字符节省空间。使用 fscanf 的返回值验证您的阅读:
if (fscanf (fp, "%255s", buf) != 1) {
fprintf (stderr, "error: fscanf read failure from 'filename'.\n");
exit (EXIT_FAILURE);
}

如果您从文件中读取的字符串可以包含空格,那么您需要修改 fscanf格式说明符,(例如 "%255[...]" ,其中字符类 [...] 描述了匹配所需信息的表达式)或使用面向行的流函数之一( fgetsgetline )从文件到 buf然后解析 buf根据文件中的需要。例如,如果文件中的第一行包含用逗号分隔的文本:
The quick brown fox, jumps over the lazy dog.

并且您需要将逗号之前出现的信息与测试字符串进行比较,您可以执行以下操作:
char str[256] = {0};    /* string to hold chars parsed from buf */
if (fgets (buf, 256, fp) == NULL) {
fprintf (stderr, "error: fgets read failure from 'filename'.\n");
exit (EXIT_FAILURE);
}

if (sscanf (buf, "%[^,]", str) != 1) {
fprintf (stderr, "error: sscanf no conversion made.\n");
exit (EXIT_FAILURE);
}

最后,比较字符串是否相等,通常的选择是使用 strcmp .如果 strcmp 比较返回,则两个字符串相等是 0 .您可以像这样实现测试:
if (strcmp (str, teststr) == 0) /* test if strings are equal */
printf ("\n string : %s\n teststr : %s EQUAL\n", str, teststr);
else
printf ("\n string : %s\n teststr : %s DIFFER\n", str, teststr);

这将使用 strcmp 执行测试并打印从文件和 teststr 读取的字符串以及字符串是 EQUAL 还是 DIFFER。

您可以按如下方式将所有部分放在一起。 (注意:程序将文件名作为第一个参数,但是通过使用简单的 ternary 运算符,如果没有给出文件名,将使用默认文件名 data.txt -- stdin 可以替换为 "data.txt" 以默认从 stdin 读取):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char **argv) {

char buf[256] = {0}; /* char array to hold 255 chars + null-terminating char */
char str[256] = {0}; /* string to hold chars parsed from buf */
char *teststr = "The quick brown fox"; /* string literal for comparison */
FILE *fp = NULL; /* FILE pointer to hold return of fopen */

fp = fopen (argc > 1 ? argv[1] : "data.txt", "r"); /* open file for reading */
if (fp == NULL) { /* if not open throw error */
fprintf (stderr, "error: file open failed 'filename'.\n");
exit (EXIT_FAILURE);
}

if (fgets (buf, 256, fp) == NULL) { /* read 1 line from file */
fprintf (stderr, "error: fgets read failure from 'filename'.\n");
exit (EXIT_FAILURE);
}
fclose (fp); /* close file when reading complete */

if (sscanf (buf, "%[^,]", str) != 1) { /* parse up to ',' into str */
fprintf (stderr, "error: sscanf no conversion made.\n");
exit (EXIT_FAILURE);
}

if (strcmp (str, teststr) == 0) /* test if strings are equal */
printf ("\n string : %s\n teststr : %s EQUAL\n", str, teststr);
else
printf ("\n string : %s\n teststr : %s DIFFER\n", str, teststr);

return 0;
}

输入文件
$ cat dat/qbfcomma.txt
The quick brown fox, jumps over the lazy dog.

使用/输出
$ ./bin/fgets_strcmp dat/qbfcomma.txt

string : The quick brown fox
teststr : The quick brown fox EQUAL

如果您有任何其他问题,请告诉我。

注:你可以简单地使用 fscanf使用格式说明符 "%255[^,]"将相同的信息读入 buf然后简单测试 buf反对 teststr无需将行解析为 str使用 sscanf .但重要的是要注意,通常的方法是使用面向行的(一次读取一行),然后从代码中的行解析所需的信息。这将避免使用 fscanf 引起的许多陷阱。或者当试图将所有模式匹配硬塞到 fscanf 中时格式说明符。它还允许从缓冲区解析所需信息的其他方法(例如使用指针算法等)。

关于c - 读取文件然后将字符串与该文件中的内容进行比较的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33990151/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com