简体   繁体   中英

reading and formatting a text file in ansi c

I'm learning C and one of the tasks it to read in a text file, and have it output a formatted text file. The final product should look like this:

1)"I must not fear.[4,17]
2)Fear is the mind-killer.[4,24]
3)Fear is the little-death that brings total obliteration.[8,56]
4)I will face my fear.[5,20]
.
.
.
13)oneWord_allAlone[1,16]
13 lines, 94 words, 481 characters
Line 10 has the most words (16)
Line 7 has the most characters (68)

I've written the code and can get something close-ish, but the information is out of order and the variables are wrong and it cuts off the first letter of each sentence. I get:

I must not fear0.) 
[4, 16]
ear is the mind-killer.0) 
[7 39]
ear is the little-death that brings total obliteration.0) 
[14 92]
.
.
.
neWord_allAlone1)
[86 470] 
1 lines, 20360 words, 110685 characters
line 1 has the most words with (86)
line 1 has the most characters with 470)    

Where it is getting 110685 characters is beyond me. So, with that said, what am I doing wrong? As far as I can tell, I have all the variables set up properly, but the output is in the wrong order, the first character is being cut off, and the counts are wayyyy off. Any help is much appreciated! Here's my code:

#include <stdio.h>

#define IN 1
#define OUT 0

void main() {

  int c = 0;
  int numChars = 0;
  int numWords = 0;
  int numLines = 0;
  int state = OUT;
  int test = 0;
  int largestNumChars = 0;
  int largestNumWords = 0;
  int totalNumChars = 0; 
  int totalNumWords = 0;
  int lineWithMostChars = 0;
  int lineWithMostWords = 0;

  FILE *doesthiswork = fopen("testWords.in", "r");
  while ((test = fgetc(doesthiswork)) != EOF) {
    if ( test == '\n') {
            ++numLines;
    }
    while ((test = fgetc(doesthiswork)) != '\n') {    
        ++numChars;
        putchar(test);   
        if (test == ' ' || test == '\t' || test == '\n') {      
          state = OUT;
        } else if (state == OUT){
          state = IN;
          ++numWords;          
        }
        totalNumWords = totalNumWords + numWords;
        totalNumChars = totalNumChars + numChars;    
     }

     if (largestNumChars == 0)  {
       largestNumChars = numChars;
     } else if (largestNumChars < numChars) {
       largestNumChars = numChars;
       lineWithMostChars = numLines;
     } else  {
       largestNumChars = largestNumChars;
       lineWithMostChars = lineWithMostChars;
     }

     if (largestNumWords == 0)  {
       largestNumWords = numWords;
       lineWithMostWords = numLines;
     } else if (largestNumWords < numWords) {
       largestNumWords = numWords;
       lineWithMostWords = lineWithMostWords;
     } else {
       largestNumWords = largestNumWords;
     }

     printf("%d) %c [%d %d]\n",numLines, test, numWords, numChars);
   }

   printf("%d lines, %d words, %d characters\n", 
     numLines, totalNumWords, totalNumChars);
   printf("line %d has the most words with (%d)\n", 
     lineWithMostWords, largestNumWords);
   printf("line %d has the most characters with (%d)\n", 
     lineWithMostChars, largestNumChars);  
}

Well, where the initial letters are going is that you're reading them with the first fgetc call, but you don't putchar them like you do with the second fgetc call.

And totalNumChars is so large because you periodically add numChars to it, but you don't ever reset numChars back to zero.

I hope this helps. Have fun finding and squishing those bugs!

First, you putchar() the letters before the line number. Then, in the second while , the test always store the '\\n' , so the [numWords, numChars] always at the next line of the letters. As @aecolley said, the numWords , numChars should be reseted back to zero.

lineWithMostWords = lineWithMostWords;

should be

 lineWithMostWords = numLines;

This is my code, maybe help you.

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

#define IN      1
#define OUT     0
#define MAXLINE 500

void main()
{
    int numChars = 0;
    int numWords = 0;
    int numLines = 0;
    int state = OUT;
    int test = 0;
    int largestNumChars = 0;
    int largestNumWords = 0;
    int totalNumChars = 0;
    int totalNumWords = 0;
    int lineWithMostChars = 0;
    int lineWithMostWords = 0;
    char line[MAXLINE+1], *lineTemp;
    int lineLen;

    FILE *doesthiswork;
    doesthiswork = fopen("testWords.in", "r");
    while (fgets(line, MAXLINE, doesthiswork) != NULL)
    {
        numChars = 0;
        numWords = 0;
        lineLen = strlen(line);
        line[lineLen - 1] = '\0';
        lineTemp = line;
        state = OUT;

        ++numLines;

        while ((test = *lineTemp++) != '\0')
        {
            ++numChars;

            if (test == ' ' || test == '\t')
            {
                state = OUT;
            }
            else if (state == OUT){
                state = IN;
                ++numWords;
            }
        }

        totalNumWords = totalNumWords + numWords;
        totalNumChars = totalNumChars + numChars;

        if (largestNumChars == 0)
        {
            largestNumChars = numChars;
         }
        else if (largestNumChars < numChars)
        {
            largestNumChars = numChars;
            lineWithMostChars = numLines;
        }

        if (largestNumWords == 0)
        {
            largestNumWords = numWords;
            lineWithMostWords = numLines;
        }
        else if (largestNumWords < numWords)
        {
            largestNumWords = numWords;
            lineWithMostWords = numLines;
        }

        printf("%d) %s [%d %d]\n",numLines, line, numWords, numChars);
    }
    printf("%d lines, %d words, %d characters\n", numLines, totalNumWords, totalNumChars);
    printf("line %d has the most words with (%d)\n", lineWithMostWords, largestNumWords);
    printf("line %d has the most characters with (%d)\n", lineWithMostChars,     largestNumChars);
}

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