1
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>

typedef struct arr_col_1{
    int x;
};

typedef struct arr_col_2{
    int x, y;
};

typedef struct array{
    int x1, y1;
};

void col_1(FILE *file, int row);
void col_2(FILE *file, int row);

int main(){
    FILE* file;
    int row = 0, col = 1, i;

    char fname[40], chr;
    int gd = DETECT, gm;

    initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
    do{
        printf("\nEnter the file which you want to vizualize = ");
        scanf("%s", &fname);
        file = fopen(fname, "r");

        if(file == NULL){
            printf("\nPlease Enter correct file name ");
        }
    }while(file == NULL);

    chr = getc(file);

    while(chr != EOF){
        if(chr == '\n'){
            row++;
        }
        if((row == 0)&&(chr == ',')){
            col++;
        }
        chr = getc(file);
    }
    printf("%d row  %d col", row, col);
    if(col == 1){
        col_1(file, row);
    }
    if(col == 2){
        col_2(file, row);
    }
    getch();
    return 0;
}


void col_1(FILE *file, int row){
    struct arr_col_1 *arr1;
    int i;
    arr1 = (struct arr_col_1*)malloc(row * sizeof(struct arr_col_1));

    for(i = 0; i < row; i++){
        fscanf(file, "%d", &arr1[i].x);
        printf("%d\n", arr1[i].x);
    }
    getch();
}

void col_2(FILE *file, int row){
    struct arr_col_2 *arr2;
    int i;

    arr2 = (struct arr_col_2*)malloc(row * sizeof(struct arr_col_2));

    for(i = 0; i < row; i++){
        fscanf(file, "%d %d", &arr2[i].x, &arr2[i].y);
        printf("%d %d\n", arr2[i].x, arr2[i].y);
    }
    getch();
}

the above code able to tell no of rows and col but it not show the data it is not able to read the data. please help me out in the above code i send file to the function but it look like i am not able to read the data. i dont know where the problem occurs please help. somewhere i think the problem lies between file pointer but code is run with no error

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    `while(chr != EOF)` after that loop the stream is at the end of the file. Need to call `rewind(file)` to get it back to the start of the file before trying to read the data. – kaylum Apr 08 '21 at 11:18
  • General notes: You should never use `scanf` or any other IO function without checking return value. Also `getc` returns an `int`, not a `char`. It is not guaranteed that `EOF` can be represented as a `char`. `scanf("%s", &fname);` Here, `fname` already decays to a pointer to first element. No need to add extra `&`. – Gerhardh Apr 08 '21 at 11:20
  • @kaylum after writting rewind only one data is able to read it not reading next data – Prasad Tupe Apr 08 '21 at 11:34
  • Please show your input together with output and expected output. Do you have 1 or 2 columns? You seem to have commas in your row. Your functions to read data only read plain integers. Also: Did you fix the missing checks for return values of IO functions, especially `fscanf`? – Gerhardh Apr 08 '21 at 12:00

0 Answers0