-1

I am searching for the occurence of the';' and symbol '.' for every new line of a text file and then to divide them for every new line. I am getting weird values for the division of every new line. Is there any other way to do it ?

 #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 Answers1

1

Look where your parenthesis are. The test for '.' == c is inside the block where ';' == c, so coma++ will never be executed. Also, you initialize dotcoma for 0 to 150, but dotcoma is defined with only 100 elements, so you are overwriting something else with zeros. Also, ferror is only tested after EOF is read, so it does not do much good. Take the time to think about when things should happen.