簡體   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