如何让我的 C 程序从文件中读取多行文本?

How can I get my C program to read more than one line of text from a file?

我正在尝试编写一个程序,从输入文件中读取文本行,重新排列单词中的字母,然后将它们写入输出文件。到目前为止我有这个:

void processFile(FILE* ifp, FILE* ofp) {
char line[1024];
char word[1024];
char* lineptr = line;
char temp;
printf("Begin file processing\n");
while (fgets(line, BIGLINE, ifp) != NULL){

    while(sscanf(lineptr,"%s",word) == true)
    {
        if (strlen(word) >= 4){
            temp = word[1];
            word[1] = word[2];
            word[2] = temp;
        }
        fputs(word,stdout);
        fputs(word,ofp);
        fputs(" ",stdout);
        fputs(" ", ofp);
        lineptr += strlen(word) + 1;

    }

}/*while*/
printf("End file processing\n");} /* processFile */

现在输出文件显示为:

<rpe><div calss="text_to_html">Project Gtuenberg The Avdentures of Sehrlock Hlomes, by Atrhur Cnoan Dyole 

但我需要它来读取我的测试文件中的所有行

<pre><div class="text_to_html">Project Gutenberg The Adventures of Sherlock Holmes, by Arthur Conan Doyle

 This eBook is for the use of anyone anywhere at no cost and with
 almost no restrictions whatsoever.  You may copy it, give it away or
 re-use it under the terms of the Project Gutenberg License included
 with this eBook or online at <a href="http://www.gutenberg.net" 
 class="_blanktarget">www.gutenberg.net</a>
 </div></pre>

我还需要确保,如果我将任何文本文件作为输入文件,它会读取所有行,而不仅仅是第一行。我怎样才能用我已经拥有的东西做到这一点?

正如我在评论中指出的那样,您的主要问题是您需要在 while (fgets(…) != NULL) 循环内重置 lineptr ,然后再开始内部循环。如果您将所有变量都放置在尽可能小的范围内,那么您就不太可能 运行 陷入这个问题——因此 temp 应该在 if 块内定义,而 wordlineptr 应该定义在外循环和内循环之间。您正在处理的第一行是最长的行,这有点不幸;这意味着 lineptr 指向一个空字节。

您应该在 fgets() 的调用中使用 sizeof(line) 而不是 BIGLINE。在计数为 1 的地方使用 true 也不合适(尽管在技术上不正确)。

这些更改产生:

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

static void processFile(FILE *ifp, FILE *ofp)
{
    char line[1024];
    printf("Begin file processing\n");
    while (fgets(line, sizeof(line), ifp) != NULL)
    {
        char word[1024];
        char *lineptr = line;
        while (sscanf(lineptr, "%s", word) == 1)
        {
            if (strlen(word) >= 4)
            {
                char temp = word[1];
                word[1] = word[2];
                word[2] = temp;
            }
            fputs(word, stdout);
            fputs(word, ofp);
            fputs(" ", stdout);
            fputs(" ", ofp);
            lineptr += strlen(word) + 1;
        }
        putchar('\n');
    }
    printf("End file processing\n");
}

int main(void)
{
    processFile(stdin, stderr);
    return 0;
}

当从 rf79.c 编译成 rf79 和 运行 并将标准错误重定向到 /dev/null 时,我得到输出:

$ ./rf79 < data 2>/dev/null
Begin file processing
<rpe><div calss="text_to_html">Project Gtuenberg The Avdentures of Sehrlock Hlomes, by Atrhur Cnoan Dyole 

Tihs eoBok is for the use of aynone aynwhere at no csot and wtih 
amlost no rsetrictions wahtsoever. You u may cpoy it, gvie it aawy or 
r-euse it udner the trems of the Porject Gtuenberg Lciense icnluded 
wtih tihs eoBok or olnine at <a herf="http://www.gutenberg.net" 
calss="_blanktarget">www.gutenberg.net</a> 
<d/iv></pre> 
End file processing
$

这看起来像你想要的。