简体   繁体   中英

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

I am trying to write a program that reads lines of texts from an input file, rearrange the letters in the words and then writes them to an output file. So far I have this:

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 */

Right now the output file reads:

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

But I need it to read all of the lines in my test file

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

I also need to make sure that if I put any text file as the input file it would read all of the lines instead of just the first. How can I do this with what I have already?

As I noted in a comment, your primary problem is that you need to reset lineptr inside the while (fgets(…) != NULL) loop before starting the inner loop. You'd be less likely to run into this problem if you placed all variables so they had the minimum possible scope — so temp should be defined inside the if block, while word and lineptr should be defined between the outer and inner loops. You're marginally unlucky that the first line you're processing is the longest line; it means that lineptr is left pointing at a null byte.

You should use sizeof(line) rather than BIGLINE in the call to fgets() . The use of true where the count is 1 is not appropriate either (though not technically incorrect).

Those changes yield:

#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;
}

When compiled from rf79.c into rf79 and run with standard error redirected to /dev/null , I get the output:

$ ./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
$

This looks like what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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