dimanche 31 août 2014

reading and formatting a text file in ansi c


Vote count:

0




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


asked 39 secs ago







reading and formatting a text file in ansi c

Aucun commentaire:

Enregistrer un commentaire