PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Issue with reading values of a 2d array from a text file
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Issue with reading values of a 2d array from a text file

Réponse
 
LinkBack Outils de la discussion
Vieux 12/05/2008, 14h42   #1
nembo kid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Issue with reading values of a 2d array from a text file

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 **************************/

  Réponse avec citation
Vieux 12/05/2008, 15h08   #2
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Issue with reading values of a 2d array from a text file

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 **************************/


  Réponse avec citation
Vieux 12/05/2008, 15h34   #3
nembo kid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Issue with reading values of a 2d array from a text file

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
  Réponse avec citation
Vieux 13/05/2008, 00h05   #4
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Issue with reading values of a 2d array from a text file

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
  Réponse avec citation
Vieux 17/05/2008, 07h49   #5
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Issue with reading values of a 2d array from a text file

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h37.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13537 seconds with 13 queries