473,378 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
13 2955
donbock
2,426 Expert 2GB
What is your question?
Nov 21 '10 #2
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
donbock
2,426 Expert 2GB
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
when i put int x[i][j], it doesnt run for some reason
Nov 21 '10 #5
must i use calloc?
Nov 21 '10 #6
weaknessforcats
9,208 Expert Mod 8TB
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
The first two, 6 and 3 are the parameters, so how do i input the numbers without that?
Nov 21 '10 #8
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
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
#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
9,208 Expert Mod 8TB
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

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

Similar topics

7
by: FrancisC | last post by:
How can I open a file, skip first 2 lines and get the 50th character? EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00 ARC 2 1 1 0 0 0 0 7 ...
5
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
2
by: coolindienc | last post by:
Hello, I have tried to fill array from a file and sort it by first column and then by second column. However, I am getting several errors. Please someone show me where is/are my mistake/s. ...
2
by: Pawel | last post by:
Howto: Programmatically serialization Entry Method Parameters ( their values ) ? See example below: public static void C(string Code, string Name) {
31
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
3
by: toffee | last post by:
Hi all, I got a pre-formatted spreadsheet. would it be possible using js to copy the data from a table on the current webpage, open the spreadsheet and paste the content ? if so, anyone got any...
4
by: sufian | last post by:
Below is the field where user enters his/her email address and the AJAX post request is sent to the server and the user sees the message: echo("<div id=\"message\" class=\"success\">Thank you! You...
5
by: unknown418 | last post by:
I am confused what is the efficient way to read in a file with multiple values per line. Each value is separated by a space character. The values could be characters or real values. I am using...
2
by: SCarpenter | last post by:
Hi, I have a subform that opens from a main form via a command button. The main form fields are populated based on a series of queries, ultimately based on tables. The subform is populated from...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.