0

I have a text file (H.txt) that looks something like this:

1 0 1 1 0 1
0 0 1 1 0 0
0 0 0 1 0 0
1 1 1 0 0 0

I need to read this text file into a 2D array called H. The size of the text file can change in length and width (i.e.. there can be more rows and more columns of binary data than the example I have above).

Heres what I have so far:

#import <stdio.h>

int main()
{

    int m = 4;
    int n = 6;
    int H[m][n];

    FILE *ptr_file;
    char buf[1000];

    ptr_file = fopen("H.txt", "r");
    if (!ptr_file)
        return 1;

    fscanf(ptr_file,"%d",H);

    fclose(ptr_file);
    return 0;
}

Any help would be appreciated.

2
  • 1
    What prevents you from continuing? Commented Oct 13, 2016 at 14:52
  • 1
    You need to use loops, two nested for loops is probably most simple an intuitive. Have you not learned going over arrays with for loops yet? Commented Oct 13, 2016 at 14:56

2 Answers 2

1

like this

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

int getRows(FILE *fp){
    int ch, rows = 0, notTop = 0;
    while((ch = getc(fp))!= EOF){
        if(ch == '\n'){
            ++rows;
            notTop = 0;
        } else
            notTop = 1;
    }
    if(notTop)
        ++rows;
    rewind(fp);
    return rows;
}

int getCols(FILE *fp){
    int ch, cols = 0, preSpace = 1;
    while((ch = getc(fp))!= EOF && ch != '\n'){
        if(isspace(ch)){
            preSpace = 1;
        } else {
            if(preSpace)
                ++cols;
            preSpace = 0;
        }
    }
    rewind(fp);
    return cols;
}

int main(void){
    int rows, cols;
    FILE *fp = fopen("H.txt", "r");
    if (!fp){
        perror("can't open H.txt\n");
        return EXIT_FAILURE;
    }
    rows = getRows(fp);
    cols = getCols(fp);
    int (*H)[cols] = malloc(sizeof(int[rows][cols]));
    if(!H){
        perror("fail malloc\n");
        exit(EXIT_FAILURE);
    }
    for(int r = 0; r < rows; ++r){
        for(int c = 0; c < cols; ++c){
            if(EOF==fscanf(fp, "%d", &H[r][c])){
                fprintf(stderr, "The data is insufficient.\n");
                free(H);
                exit(EXIT_FAILURE);
            }
        }
    }
    fclose(fp);
    //test print
    for(int r = 0; r < rows; ++r){
        for(int c = 0; c < cols; ++c){
            printf("%d ", H[r][c]);
        }
        puts("");
    }

    free(H);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

There are many ways to solve your problem. One very simple way would be to have two loops, one nested inside the other. The outer for the lines and the inner for the columns. In the inner loop you read one number and store into the correct place of your matrix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.