简体   繁体   English

逐行读取文件-为什么我只有第一行?

[英]Reading file line by line - why I get the first line only?

I have a task in university to write a C program which reads a file and counts the number of single and multi comments. 我在大学里有一个任务要编写一个C程序,该程序读取一个文件并计算单注释和多注释的数量。 The problem I have is that the second while() only reads the first line and so the returned comments are 0. 我的问题是第二个while()仅读取第一行,因此返回的注释为0。

Previously I read the file character by character, but that's not the task requirement. 以前我逐个字符地读取文件,但这不是任务要求。 Why does this program read only the first line and not the others? 为什么该程序只读取第一行而不读取其他行?

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

要枚举行上的字符,必须将每行的i重新初始化为0,并在空终止符或换行符处停止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM