【问题标题】:How to read multiple strings on the same row of a file?如何读取文件同一行上的多个字符串?
【发布时间】:2015-01-08 01:22:17
【问题描述】:

我是 C 新手,正在尝试学习字符串、I/O 和文件。 我正在尝试从每行包含多个字符串的文件中读取字符串,如下所示:

str1 str2 str3
str4 str5 str6
str7 str8 str9

到目前为止,我已经尝试过:

scanf("%s %s %s", &str1, &str2, &str3);

但它以一种奇怪的方式读取字符串。为什么scanf 功能不起作用?我还看到另一种方法是使用fgets 读取缓冲区中的整行,但我对自己的缓冲区管理技能不是很有信心,那么有没有办法使用scanf 来做到这一点?

【问题讨论】:

  • 把str1, str2和str4的定义贴出来就好了。
  • 请澄清“奇怪”,也许通过展示一个或多个例子。
  • 如果我要侮辱你,那是因为你提供的细节太差了。 str1 是什么? “奇怪的方式”到底是什么意思?您没有提供足够的详细信息或有助于程序员解决问题的信息。
  • 最好不要使用gets(),但您确实需要发布更多您尝试过的内容以及失败的原因。

标签: c string scanf fgets


【解决方案1】:

在 C 中读取行的最佳方法是首先使用 fgets() 读取,然后您可以使用 sscanf() 或 strtok() 解析行以获取单词。

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

int main(){
    char line[1024];
    char str1[1024], str2[1024], str3[1024];
    while (fgets(line, sizeof(line), stdin) != NULL){
        if (strlen(line) > 0 && line[strlen(line) - 1] != '\n') {
            // or line[0] instead of strlen(line) > 0
            // like chux suggested (line[0] is more efficient)
            puts("The line is longer than expected");
            return 1;
        }
        if (sscanf(line, "%s %s %s", str1, str2, str3) != 3){
            // notice it wont fail if the input have more than 3 columns
            puts("Error parsing, not enough columns");
            return 2;
        }
    }
    return EXIT_SUCCESS;
}

【讨论】:

  • 为什么投反对票?正确阅读scanf() 比学习如何使用fgets() 更难。
  • 2 个次要位:1) 考虑fgets(line, sizeof line, stdin) 2) 在极少数情况下嵌入'\0',[strlen(line) 可能为0。最好在使用line[strlen(line) - 1] 之前使用insure strlen(line) &gt; 0。也许if (line[0] &amp;&amp; line[strlen(line) - 1] != '\n')
  • 谢谢@chux 我已将您的建议添加到我的回答中。
【解决方案2】:

我有一个文本文件:

ho hello blah hi dsdf hihi hiho hih bleh

以下是我用来阅读它的代码。

#include <stdio.h>

int main()
{
    char string1[11];   
    char string2[11];
    char string3[11];
    char string4[11];
    char string5[11];
    char string6[11];
    char string7[11];
    char string8[11];
    char string9[11];

    FILE * fileReader = fopen("text.txt", "r"); 
    if(fileReader)
    {
        fscanf(fileReader, "%10s %10s %10s %10s %10s %10s %10s %10s %10s", string1, string2, string3, string4, string5, string6, string7, string8, string9);
        printf("Found: %s %s %s %s %s %s %s %s %s\n", string1, string2, string3, string4, string5, string6, string7, string8, string9);
        fclose(fileReader);
    }
    else
    {
        puts("Error opening filestream!");
    }
 return 0;
}

FILE *,又名流用于 C 中的输入/输出。scanf() 使用默认输入流 (stdin),因此不能用于读取文件。 fscanf() 允许您将文件流指定为参数。

另外,使用 %10s 可以防止 fscanf() 为每个字符串读取十个以上的字符。如果没有 %10s,scanf() 可能会导致您的程序出现缓冲区溢出。这基本上是当它读取的数据超过 char [] 变量可以容纳的数据时,会破坏程序内存。

if(fileReader) 检查 fileReader 是否已成功打开(非零值 = true,0(又名 NULL)= false)。我可以像 if(fileReader != NULL) 那样做,但效果是一样的。

此外,在处理数组时不要使用 & 运算符。 数组在传递给像 scanf() 这样的函数时会退化为指针,因此使用 & 时,您将向 scanf() 传递指针的地址,这不是您想要的。

这是我编译上述代码时得到的(使用的文本文件保存为“text.txt”):

sky@sky-Lenovo-3000-N500:~$ gcc stringReader.c -o string && ./string
Found: ho hello blah hi dsdf hihi hiho hih bleh

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多