Reputation: 3
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?
Upvotes: 0
Views: 65
Reputation: 753675
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.
Upvotes: 1