Hi, basically im trying to read in a file that represents a matrix, where the first line is in the format of <number of rows> <number of collumns> then all lines after that follow the format
<row> <col> <value> i.e. the file would look like
2 2
1 1 2
1 2 3
would represent the matrix
| 2 3 |
| 0 0 |
im having trouble using fscanf to read the numbers into a 2d array. I get a bus error when I try to fscanf the second line, Ive used ftell to check the position of the stream and it seems to be in the right place but it never works. My code looks like this:
int main (int argc, char * argv[])
{
FILE *file;
file = fopen(argv[1], "r");
int rows, cols, x, y, z;
fscanf(file, "%d %d", &rows, &cols);
int matrix[rows][cols];
fscanf(file, "\n%d %d %d", x, y, z); //this is where the problem is.
fclose(file);
}
many thanks
Dan
1 5998
Read this and posty again of you still have problems:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
That is an array of 5 elements each of which is an int.
won't compile. You need to declare the number of elements.
Second, this array:
is still an array of 5 elements. Each element is an array of 10 int.
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int: -
int array[5][10];
-
-
int (*ptr)[10] = array;
-
Fourth, when the number of elements is not known at compile time, you create the array dynamically: -
int* array = new int[value];
-
int (*ptr)[10] = new int[value][10];
-
int (*ptr)[10][15] = new int[value][10][15];
-
In each case value is the number of elements. Any other brackets only describe the elements.
Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code: -
int** ptr = new int[value][10]; //ERROR
-
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise: -
int*** ptr = new int[value][10][15]; //ERROR
-
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
With the above in mind this array: -
int array[10] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Wheras this array: -
int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Kinda the same, right?
So if your disc file contains
0 1 2 3 4 5 6 7 8 9
Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.
Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Benedicte |
last post by:
Hi,
I'm getting some problems when using fscanf to read a file.
This is a piece of the program code:
main ()
{
/*** Variable declaration ***/
FILE *vpfile; /*** Data file ***/
|
by: Psibur |
last post by:
Hello, trying to get back into c and was having issue with reading a
simple text file with an aribtrary # of lines with 3 int's per line,
with the eventual purpose of putting each int into an...
|
by: Thomas Sourmail |
last post by:
Hi,
I hope I am missing something simple, but.. here is my problem:
I need my program to check the last column of a file, as in :
a b c d target ref
0 0 0 0 1 a
1 0 0 0 1.5 b
2 0 0 0 2 c
|
by: siliconwafer |
last post by:
Hi All,
here is one code:
int main() {
FILE*fp;
unsigned long a;
fp = fopen("my_file.txt","w+");
a = 24;
fprintf(fp,"%ld",a);
while(fscanf(fp,"%ld",&a) == 1) {
|
by: quyvle |
last post by:
I can't seem to get this function to work correctly. I'm wondering
if anyone could help me out with this.
So I'm using the fscanf function to read the input stream and store
each string in the...
|
by: kvnsmnsn |
last post by:
Over the course of my career I've transitioned from an Ada programmer
(am I dating myself?) to a C programmer to a Java programmer and now
back to a C programmer with the job I've currently...
|
by: PeterOut |
last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
I am not sure if this is a C, C++ or MS issue but fscanf has been
randomly hanging on me. I make the call hundreds, if not thousands,
of...
|
by: David Mathog |
last post by:
Apologies if this is in the FAQ. I looked, but didn't find it.
In a particular program the input read from a file is supposed to be:
+ 100 200 name1
- 101 201 name2
It is parsed by reading...
|
by: momotaro |
last post by:
I have a small problem with the last fscanf in this function...every thing is logic in there but can't find the problem... plz help
node *BuildGraph()
{
int range, vehicules, i, j,...
|
by: Bill Cunningham |
last post by:
I'm doing something wrong and all I know to do is turn to clc. I have a
text file containing 2 doubles separated by a tab.
..26 0
Is the text. I want to read the two double and printf them...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
| |