English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i need help reagarding coding in C.
I want to read a file from disk ...
that has X rows and Y coloumns...
with numbers in it as data...
and store those numbers into an array using C.

Very urgent ...

2007-02-07 10:52:46 · 2 answers · asked by sri 1 in Computers & Internet Programming & Design

2 answers

Note: "bytekhan" forgot fclose(f); at the end.

2007-02-08 01:34:04 · answer #1 · answered by justme 7 · 0 0

assuming you have an ascii file (as opposed to a binary file) and lets say its a 4x4, you could do the following (if I remember correctly):

#include

#define Xsize 4
#define Ysize 4
void main() {
FILE *f
int M[Xsize][Ysize];
int x;
int y;

f = fopen("my_data_file", "r");

for (x = 0; x for (y = 0; y fscanf("%d", &M[x][y]);
}
}
}

Or something like this but I think you get the picture. If you don't know the exact size (rows and columns), you'll have to do some dynamic memory allocation using malloc().

2007-02-07 19:09:43 · answer #2 · answered by bytekhan 2 · 1 0

fedest.com, questions and answers