繁体   English   中英

如何使我的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 如果放置所有变量,使它们具有尽可能小的范围,则不太可能遇到此问题-因此,应该在if块内定义temp ,而应该在外部和内部循环之间定义wordlineptr 您不太幸运,您要处理的第一行是最长的行; 这意味着lineptr指向空字节。

您应该在对fgets()的调用中使用sizeof(line)而不是BIGLINE 在计数为1true下使用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
$

这看起来像您想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM