|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
This function crashes when called from main.
It should read int elements of a 2d array (mat[][DIMCOL]) Each element is placed on a line. The first line (first element) is the numbers of rows. The second line (second element) is the number of cols. Next lines holds the elements sorted sequentially. Was wondering where I'm mistaking. Thanks in advance. /***************** Code starts here *************************/ int matfilein (int mat[][DIMCOL], char filename[], int* nrows, int* ncols) { FILE *fpin; int j=0; int i=0; int m, n; /* Holds locally number of rows and cols */ fpin=fopen(filename, "r"); if (fpin==NULL) { fprintf(stderr, "\nFile opening: %s failed\n", filename); return 0; /* Reading failed */ } fscanf(fpin, "%d\n", &m); /* The first elem. is rows number */ fscanf(fpin, "%d\n", &n); /* The second elem. is cols number */ for (i=0;i<m;i++) { for (j=0;i<n;j++) { fscanf(fpin, "%d\n", &mat[i][j]); } } fclose(fpin); /* Close the file */ *nrows = m; /* Pass by address the number of rows */ *ncols = n; /* Pass by address the number of cols */ return 1; /* Reading successfully */ } /******************** Code ends here **************************/ |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
nembo kid wrote:
> This function crashes when called from main. > > It should read int elements of a 2d array (mat[][DIMCOL]) > > Each element is placed on a line. > > The first line (first element) is the numbers of rows. > The second line (second element) is the number of cols. > Next lines holds the elements sorted sequentially. > > Was wondering where I'm mistaking. > > Thanks in advance. > > /***************** Code starts here *************************/ > int > matfilein (int mat[][DIMCOL], char filename[], > int* nrows, int* ncols) > { > > FILE *fpin; > int j=0; > int i=0; > int m, n; /* Holds locally number of rows and cols */ > > fpin=fopen(filename, "r"); > > if (fpin==NULL) > { > fprintf(stderr, "\nFile opening: %s failed\n", filename); > return 0; /* Reading failed */ > } > > fscanf(fpin, "%d\n", &m); /* The first elem. is rows number */ > > fscanf(fpin, "%d\n", &n); /* The second elem. is cols number */ I hope your real code check all fopen and fscanf statements for success before proceeding? > for (i=0;i<m;i++) > { > for (j=0;i<n;j++) Shouldn't the test expression be j < n? > { > fscanf(fpin, "%d\n", &mat[i][j]); You should place a bounds check before this fscanf here. > } > } > > fclose(fpin); /* Close the file */ > > *nrows = m; /* Pass by address the number of rows */ > *ncols = n; /* Pass by address the number of cols */ > > return 1; /* Reading successfully */ > } > > /******************** Code ends here **************************/ |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
santosh ha scritto:
> Shouldn't the test expression be j < n? Ops...fixed ;-) P.S. Bound check on scanf? Should I have to avoid numbers longer than MAXINT?? Thanks again |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
nembo kid wrote:
> santosh ha scritto: > >> Shouldn't the test expression be j < n? > > Ops...fixed ;-) > > P.S. Bound check on scanf? Should I have to avoid numbers longer than > MAXINT?? A well written program should be able to play straight man to two kittens fighting on the keyboard. -- pete |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On May 12, 6:42am, nembo kid <nembo@kid> wrote:
> This function crashes when called from main. > > It should read int elements of a 2d array (mat[][DIMCOL]) > > Each element is placed on a line. > > The first line (first element) is the numbers of rows. > The second line (second element) is the number of cols. > Next lines holds the elements sorted sequentially. > > Was wondering where I'm mistaking. > > Thanks in advance. > > /***************** Code starts here *************************/ > int > matfilein (int mat[][DIMCOL], char filename[], > int* nrows, int* ncols) > { You need to show us the code that calls this function, including the definition of all the arguments. |
|
![]() |
| Outils de la discussion | |
|
|