2016-12-05 36 views
0

我需要製作一個程序,提示用戶輸入兩個文本文件的名稱,這兩個文本文件將在屏幕上顯示並顯示在屏幕上,然後顯示其統計信息,如數字字符,單詞和行。我已經設法從統計部分開始工作。他們似乎沒有計算在內,我認爲這與我使用的while語句有關。任何幫助將是巨大的:)AC程序在兩個文件中讀取並在屏幕上顯示其統計信息

代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 
#include <string.h> 

int main() { 

    // declaring variables 
    FILE *fp; 
    int charcount = 0, wordcount = 0, linecount = 0; 
    int character; 
    char first[50]; 
    char second[50]; 
    char ch[200]; 

    // asking the user for the file names and scanning the names they enter as txt files 
    printf(" Enter the first file name: "); 
    scanf("%s", first); 
    strcat(first, ".txt"); 

    printf(" Enter the second file name: "); 
    scanf("%s", second); 
    strcat(second, ".txt"); 

    // opening the file stream 
    fp = fopen(first, "r"); 

    // if the file cannot be reached, display error 
    if (fp == NULL) { 
     printf("File cannot be opened: %s\n", first); 
     return 0; 
    } 

    // reading and printing the file into the program 
    printf("\n---FIRST FILE---\n"); 
    while (!feof(fp)) { 
     fgets(ch, 200, fp); 
     puts(ch); 
    } 

    // counting the characters, words and lines until the program is finished 
    while ((character = getc(fp)) != EOF) { 
     if (character == '-') 
     { 
      charcount++; 
     } 
     if (character == ' ') 
     { 
      wordcount++; 
     } 
     if (character == '\n') 
     { 
      linecount++; 
     } 
    } 

    // closing the stream 
    fclose(fp); 

    // printing the number of characters, words and lines 
    printf("\n Characters: %d \n Words: %d\n Lines: %d\n\n\n", charcount, wordcount, linecount); 

    //---------SECOND FILE----------// 

    // opening the stream 
    fp = fopen(second, "r"); 

    // reading and printing the file into the program 
    printf("\n---SECOND FILE---\n"); 
    while (!feof(fp)) { 
     fgets(ch, 200, fp); 
     puts(ch); 
    } 

    // counting the characters, words and lines until the program is finished 
    while ((character = getc(fp)) != EOF) { 
     if (character == '-') 
     { 
      charcount++; 
     } 
     if (character == ' ') 
     { 
      wordcount++; 
     } 
     if (character == '\n') 
     { 
      linecount++; 
     } 
    } 

    // closing the stream 
    fclose(fp); 

    // printing the number of characters, words and lines 
    printf("\n Characters: %d \n Words: %d\n Lines: %d\n\n", charcount, wordcount, linecount); 

} 
+2

歡迎堆棧溢出!這聽起來像你可能需要學習如何使用調試器來遍歷代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。進一步閱讀:[如何調試小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –

+2

請參閱:[爲什麼「while(!feof(file))」總是出錯?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong)。 –

+0

在第二個while循環之前,您需要倒帶(fp);還有'puts(ch);' - >'fputs(ch,stdout);' – BLUEPIXY

回答

0

試想一下,你正在使用的文本編輯器之一打開你的文件,並移動插入/光標是導航的唯一途徑。你的第一個目標是瀏覽整個內容並顯示它。這就是下面的循環作用:

while(!feof(fp)){ 
    fgets(ch, 200, fp); 
    puts(ch); 
} 

!feof(fp)光標移動到文件的末尾,所以你可以閱讀這一切。

如果你必須計算字符,那麼你需要導航回你的文件中的某個地方。既然你需要整個txt的統計信息,那麼你可以在第二次循環之前簡單地使用rewind(fp)fseek(fp, 0, SEEK_SET)將光標移回開始。

我推薦使用fseek(),因爲它會清除文件結束指示符。 仔細查看here

+0

非常感謝您的幫助! 我已經在包含if語句的第二個while循環之前添加了fseek(fp,0,SEEK_SET),但它仍然無法正確計數字符,單詞或行。還有什麼想法? – rozak

0

我終於得到它的工作,如果有人有興趣最終的代碼是在這裏:

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 
#include <string.h> 

int main() { 

    // declaring variables 
    FILE *fp; 
    int charcount = 0, wordcount = 0, linecount = 1; 
    int charcount2 = 0, wordcount2 = 0, linecount2 = 1; 
    int character; 
    char first[50]; 
    char second[50]; 
    char ch[200]; 

    // asking the user for the file names and scanning the names they enter as txt files 
    printf(" Enter the first file name: "); 
    scanf("%s", first); 
    strcat(first, ".txt"); 

    printf(" Enter the second file name: "); 
    scanf("%s", second); 
    strcat(second, ".txt"); 

    // opening the file stream 
    fp = fopen(first, "r"); 

    // if the file cannot be reached, display error 
    if (fp == NULL) { 
     printf("File cannot be opened: %s\n", first); 
     return 0; 
    } 

    // reading and printing the file into the program 
    printf("\n---FIRST FILE---\n"); 
    while (!feof(fp)) { 
     fgets(ch, 200, fp); 
     fputs(ch, stdout); 
    } 

    // counting the characters, words and lines until the program is finished 
    fseek(fp, 0, SEEK_SET); 
    while ((character = fgetc(fp)) != EOF) { 
     if (character == EOF) 
      break; 
     { 
      charcount++; 
     } 
     if (character == ' ' || character == '.') 
     { 
      wordcount++; 
     } 
     if (character == '\n') 
     { 
      linecount++; 
     } 
    } 

    // closing the stream 
    fclose(fp); 

    // printing the number of characters, words and lines 
    printf("\n Characters: %d \n Words: %d\n Lines: %d\n\n\n", charcount, wordcount, linecount); 

    //---------SECOND FILE----------// 

    // opening the stream 
    fp = fopen(second, "r"); 

    // reading and printing the file into the program 
    printf("\n---SECOND FILE---\n"); 
    while (!feof(fp)) { 
     fgets(ch, 200, fp); 
     fputs(ch, stdout); 
    } 

    // counting the characters, words and lines until the program is finished 
    fseek(fp, 0, SEEK_SET); 
    while ((character = getc(fp)) != EOF) { 
     if (character == EOF) 
      break; 
     { 
      charcount2++; 
     } 
     if (character == ' ' || character == '.') 
     { 
      wordcount2++; 
     } 
     if (character == '\n') 
     { 
      linecount2++; 
     } 
    } 

    // closing the stream 
    fclose(fp); 

    // printing the number of characters, words and lines 
    printf("\n Characters: %d \n Words: %d\n Lines: %d\n\n", charcount2, wordcount2, linecount2); 

} 
0

計數抽樣

#include <stdio.h> 
#include <ctype.h> 

typedef struct statistics { 
    size_t charcount, wordcount, linecount; 
} Statistics; 

Statistics count_cwl(FILE *fp){ 
    size_t c = 0, w = 0, l = 0; 
    int ch; 
    char prev = ' '; 

    while((ch = getc(fp)) != EOF){ 
     ++c; 
     if(isspace(prev) && !isspace(ch)){ 
      ++w;//need to delete punctuation marks ? 
     } 
     if(ch == '\n'){ 
      ++l; 
     } 
     prev = ch; 
    } 
    if(prev != '\n')//There is no newline at the end of the file 
     ++l; 
    return (Statistics){ c, w, l}; 
} 

int main(void) { 
    char filename[50] = "test.c"; 
    FILE *fp = fopen(filename, "r"); 

    if (fp == NULL) { 
     printf("File cannot be opened: %s\n", filename); 
     return -1; 
    } 

    Statistics stat = count_cwl(fp); 

    fclose(fp); 

    printf("\nCharacters: %zu\nWords: %zu\nLines: %zu\n", stat.charcount, stat.wordcount, stat.linecount); 
} 
相關問題