By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

Filling a 2d array from file, where the parameters are the first two values

P: 8
The data file is
6 3
6 7 8
1 2 3
7 8 9
3 4 5
8 9 0
1 6 8
------------
#include <stdio.h>
#include <stdlib.h>

int
main (void)
{
int a, b,i=0,j=0, x[i][j];
FILE *f1;

f1 = fopen ("mat1.dat", "r");

fscanf (f1, "%d%d", &a, &b);

printf ("Rows: %d, Columns: %d\n", a, b);



for (int i=0; i<a; i=i+1)
{
for (int j=0; j<b; j=j+1)
{
fscanf (f1, "%d", &x[i][j]);
}
printf ("\n");
}

fclose (f1);



return (0);
}

It doesnt work. How do i read the matrix and ignore the first two values? Is the code correct?
Nov 21 '10 #1
Share this Question
Share on Google+
13 Replies


Expert 100+
P: 2,396
What is your question?
Nov 21 '10 #2

P: 8
I keep getting
1 6 8
1 6 8
1 6 8
1 6 8
1 6 8
1 6 8
Nov 21 '10 #3

Expert 100+
P: 2,396
Take a look at Arrays Revealed.

Are you using C or C++?
If C, do you know if you are using C99?

You declare x as having 0 rows and 0 columns, but then you go on to store values in lots of rows and columns.
Nov 21 '10 #4

P: 8
when i put int x[i][j], it doesnt run for some reason
Nov 21 '10 #5

P: 8
must i use calloc?
Nov 21 '10 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
Uisng x[i][j] is OK.

What's not OK is that your array does not have 6 elements of 3 ints per element to match your incoming data.
Nov 21 '10 #7

P: 8
The first two, 6 and 3 are the parameters, so how do i input the numbers without that?
Nov 21 '10 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
Did you read the article "Arrays Revealed" that donbock ask you to do?

The solution to your problem is about the last example in the article- and it's coded for you already.

Take a look and post again if you are still stuck.
Nov 21 '10 #9

P: 8
I am really not understanding it. Why doesnt it work when i put it as x[i][j] and why does it only scan the last line of the file? It was done as such in class, and i dont know if i need to use pointers
Nov 21 '10 #10

weaknessforcats
Expert Mod 5K+
P: 9,197
Yes you will need to use pointers. The dimension ofyour array is 6 rows of 3 int. That makes 18 int.

Your first job is to allocate an array of 18 int.

Then you typecast a pointer to that array as a pointer to an array of 6 elements with 3 int per element.

This is truly coded for you in that Arrays Revealed article.
Nov 22 '10 #11

P: 8
I have tried that, the problem im having isnt with creating such an array. Once i have created it, it doesnt seem to work, even with the use of pointers. The problem is scanning the numbers from the file.
Nov 22 '10 #12

P: 8
#include <stdio.h>
#include <stdlib.h>

int
main (void)
{
int nrows, ncols,i,j;
int *numbers;
FILE *f1;

f1 = fopen ("mat1.dat", "r");

fscanf (f1, "%d%d", &nrows, &ncols);

printf ("Rows: %d, Columns: %d\n", nrows, ncols);

/* allocating the array of integers */
numbers = (int *) calloc (nrows*ncols, sizeof(int));

/*for (i=0; i<nrows; i=i+1)
{
for (j=0; j<ncols; j=j+1)
{
fscanf (f1, "%3d", numbers[i*ncols+j]);
}
}
*/
for (i=0; i<nrows; i=i+1)
{
for (j=0; j<ncols; j=j+1)
{
printf ("%3d ", numbers[i*ncols+j]);

}
printf ("\n");
}

free (numbers);
fclose (f1);



return (0);
}
In this, everything works other than the inputting of the data from the file. I can create a 6/3 matrix. But when its being filled, it wont run
Nov 22 '10 #13

weaknessforcats
Expert Mod 5K+
P: 9,197
Try this:

Expand|Select|Wrap|Line Numbers
  1. fscanf (f1, "%d", &numbers[i*ncols+j]);
With fscanf, you need the address of the target.
Nov 22 '10 #14

Post your reply

Sign in to post your reply or Sign up for a free account.