2014-09-01 1 views
0

저는 C 및 텍스트 파일에서 읽을 작업 중 하나를 배우고 형식이 지정된 텍스트 파일을 출력합니다. 내가 코드를 작성했습니다 닫습니다 틱 뭔가를 얻을 수ansi c에서 텍스트 파일 읽기 및 서식 지정

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

110685의 문자를 얻는 곳은 나다. 그래서, 그렇게 말하면, 내가 뭘 잘못하고 있니? 내가 말할 수있는 한, 모든 변수가 올바르게 설정되었지만, 출력이 잘못된 순서로되어 있고, 첫 번째 문자가 잘리고 카운트가 꺼져 있습니다. 어떤 도움을 많이 주시면 감사하겠습니다!

#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); 
} 
+2

다른 사람이 자신의 코드를 읽을 수있게하려면 (절망에 손을 댄 채 반대로) * 들여 쓰기를 일관되고 정확하게 정렬하십시오. 예를 들어, 두 번째 while 루프가 어디에서 끝나는 지 처음에는 명확하지 않습니다. –

+0

'FILE * doesthiswork doesthiswork = fopen ("testWords.in", "r")은 무엇인가? 나는 이것이 당신의 진짜 코드라고 생각하지 않습니다. –

+0

사실은 제 실제 코드입니다. 파일 이름이 필요하고 작동하는지 확신 할 수 없기 때문에 출력 파일의 이름을 지정했습니다. – user3579225

답변

2

음, 초기 문자가는 첫 번째 fgetc 호출을 읽는 것으로되어 있지만, 두 번째 fgetc 호출처럼 당신이하지 putchar을 수행 여기 내 코드입니다.

그리고 totalNumChars은 주기적으로 numChars을 추가하지만 numChars을 다시 0으로 재설정하지 않기 때문에 너무 큽니다.

이 정보가 도움이되기를 바랍니다. 그 버그를 찾아 내고 squilling하는 재미를보십시오!

0

먼저 행 번호 앞에있는 문자는 putchar()입니다. 그런 다음 두 번째 while에서 test은 항상 '\n'을 저장하므로 항상 다음 줄에 [numWords, numChars]이 저장됩니다. @aecolley가 말했듯이 numWords, numChars은 다시 0으로 재설정해야합니다.

lineWithMostWords = lineWithMostWords; 

어쩌면 당신을 도와, 이건 내 코드입니다

lineWithMostWords = numLines; 

해야한다.

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