搜索符号';'的出现和符号'。'对于文本文件的每个新行

时间:2014-12-15 20:59:07

标签: c arrays text statements

我正在寻找&#39 ;;'和符号'。'对于文本文件的每个新行,然后为每个新行划分它们。我为每一条新线的划分得到了奇怪的价值。还有其他办法吗?

 #include <string.h>
#include <conio.h>/* For exit() function */

int main()
{
    char file_name[1000];
    FILE *file2 = 0;

    gets(file_name);


    {
        int rows = 1;//broq na vsichki redove
        int simvoli[150];//broq na simvolite na daden red
        int coma;//broq na vs simvoli
        int dotcoma[100];
        int j;
        char c;

        file2 = fopen(file_name, "r");//otvarq faial za chetene
        if (file2 == NULL){
            printf("Cannot open %s\n", file_name);
            exit(2);
        }//if 
 for (j = 0; j < 150; j++)
    dotcoma[j] = 0;
    coma = 0;
    do{
    c = fgetc(file2);
    if (c == '\n'){
    rows++;
    }
    if (';' == c){
    dotcoma[rows - 1]++;

    if ('.' == c)
    {
    coma++;

    }}

    } while (c != EOF);   //chete do kraq na faila

    if (ferror(file2)){
    printf("Error reading file.\n");

    }//if

    printf("The number of the symbol ; on a row / the number of symbol . on a row: ");
    for (j = 0; j < rows; j++){
    printf("Row %d: %f\n", j + 1, (float)dotcoma[j] / coma);

    } 

1 个答案:

答案 0 :(得分:1)

查看括号的位置。测试'。' == c在块里面';' == c,所以昏迷++永远不会被执行。此外,您将dotcoma初始化为0到150,但是dotcoma仅定义了100个元素,因此您将用零覆盖其他内容。此外,只有在读取EOF后才会测试ferror,因此它没有太大的好处。花点时间考虑什么时候会发生。