473,499 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jun 27 '08 #1
4 1336
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 **************************/
Jun 27 '08 #2
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
Jun 27 '08 #3
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
Jun 27 '08 #4
On May 12, 6:42*am, nembo kid <nembo@kidwrote:
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.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1367
by: Row | last post by:
HI, I would first like to say its been about 3 years since looking at java im very rusty! I have to write a post it notes type applet which will function online. (reading from a flat text file)...
7
14635
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
8
9498
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
9
6726
by: jeff M via .NET 247 | last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I...
18
9016
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
1
3358
by: mpar612 | last post by:
Hi everyone, I'm not sure if this is asking too much or not. I am trying to get the following PHP code to work on my website. It utilizes PHP 5, MySQL 4.1 and the PEAR DB module. I am...
6
5237
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
2508
by: baggymccloy | last post by:
I need to be able to make a program (in C) that can store stuff from a text file into an array. I wanna have so it so that if the text file had "abcdefg123456!@#$%^ 234234asdfa#$%#$%#" inside...
21
3010
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.