2016-12-06 3 views
0

Ich habe eine Aufgabe in der Universität, ein C-Programm zu schreiben, das eine Datei liest und die Anzahl der Einzel- und Mehrfachkommentare zählt. Das Problem, das ich habe, ist, dass die zweite while() nur liest die erste Zeile und so die zurückgegebenen Kommentare sind 0.Datei zeilenweise lesen - warum bekomme ich nur die erste Zeile?

Zuvor habe ich die Datei Zeichen für Zeichen, aber das ist nicht die Aufgabe Anforderung. Warum liest dieses Programm nur die erste Zeile und nicht die anderen? > `Strlen (line -

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

int main(int argc, char **argv) { 
    FILE *fp; 
    int c, i = 0; 
    char path[256], ch, line[80]; 
    unsigned int multi = 0; 
    unsigned int single = 0; 

    enum states { 
     PLAIN_TEXT, 
     SLASH, 
     STAR, 
     SINGLE_COMMENT, 
     MULTI_COMMENT, 
     QUOTES 
    } state = PLAIN_TEXT; 

    printf("Write file's name\n"); 
    gets(path) 

    fp = fopen(path, "r"); 
    if (!fp) { 
     // give an error message 
    } else {   
     while (fgets(line, sizeof(line), fp) != NULL) {   
      while (i < sizeof(line)) { 
       printf("%d.%c", i, line[i]); 
       switch (state) { 
        case PLAIN_TEXT: 
        switch (line[i]) { 
         case '/': i++; 
         state = SLASH; 
         break; // found a slash. In the next loop the switch argument will be SLASH 
         case '"': i++; 
         state = QUOTES; 
         break; // found a quote. Quoted text (there might be a '//' inside) 
         default: i++; 
         break; // found an ordinary character 
        } 
        break; 
        case QUOTES: 
        switch (line[i]) { 
         case '"': i++; 
         state = PLAIN_TEXT; 
         break; // Gets out the string; 
         case ' ':i++; 
         state = PLAIN_TEXT; 
         break; 
         default: i++; 
         state = QUOTES; 
         break; // Still a quoted text; 
        } 
        break; 
        case SLASH: 
        switch (line[i]) { 
         case '/': i++; 
         state = SINGLE_COMMENT; 
         break; // found a slash => a possible single comment found 
         case '*': i++; 
         state = MULTI_COMMENT; 
         break; // found a star => a possible multi comment found 
         default: i++; 
         state = PLAIN_TEXT; 
         break; // found an ordinary character 
        } 
        break; 
        case STAR: 
        switch (line[i]) { 
         case '/': i++; 
         state = PLAIN_TEXT; 
         multi++; 
         break; // Increments the multi comment and the next characher will be treated as a plain_taxt 
         default: i++; 
         state = MULTI_COMMENT; 
         break; // Still multi comment 
        } 
        break; 
        case SINGLE_COMMENT: 
        switch (line[i]) { 
         case '\n':i++; 
         state = PLAIN_TEXT; 
         single++; 
         break; // End of the single comment line. Increment the counter and the next character will be treated as a plain_text 
         default: i++; 
         break; 
        } 
        break; 
        case MULTI_COMMENT: 
        switch (line[i]) { 
         case '*': i++; 
         state = STAR; 
         break; // Found a multi comment. The next state will be star. 
         default: i++; 
         break; 
        } 
        break; 
        default: i++; 
        break; 
       } 
      } 
     } 
     fclose(fp); 
     printf("Single-comment : %8u\n", single); 
     printf("Multi-comment : %8u\n", multi); 
    } 
    return 0; 
} 
+1

' sizeof (Linie) 'neu initialisieren) '. 'sizeof (line)' ist die Größe des Puffers (80) in Ihrem Fall und nicht die Länge des gerade gelesenen Strings mit 'fgets'. Es gibt höchstwahrscheinlich auch andere Probleme. –

+2

auch, wo setzt du 'i' zurück? –

+0

@ Jean-FrançoisFabre Ich setze 'i' nicht zurück. Wo soll ich das machen? – Alex

Antwort

1

Um die Zeichen auf der Linie aufzuzählen, müssen Sie i-0 für jede Zeile und halten am Nullabschluss oder am Zeilenende-Zeichen

Verwandte Themen