Thread: Read into dynamic array.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Read into dynamic array.

    Hi all,
    I'm sitting and tring to read a matrix into a dynamic array. Does anyone have a smart way of doing this?

    Thanks for oyur time.
    Esben.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm thinking of something like a loop. Maybe combine with some form of reading function...


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Sarcasm is always funny...

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by esben
    Sarcasm is always funny...
    He actually gave you good advice. Let's see what you've done so far...

    - xeddiex

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Hi.
    Yeah I guess using a loop and a read function is a pretty good advice, since it's the only way (as far as I know) to read in a vector or matrix, but I was kind of hoping for some more specific info. This is how I ended up reading the input file:
    Code:
            int elements = 0;
    	int i = 0;
    	int j = 0;
    	int DataMatrix[row][col];
    	char temp = 0;
    
    	InFile = fopen("DWTinput.txt", "r");
    
    	//Read data into matrix:
    	for(j = 0; j < col; j++){
    		i = 0;
    		while( i < row){
    			n = fscanf(InFile, "%d", &temp); //If I use fgetc, the ascii values are returned.
    			elements += n;
    			DataMatrix[i][j] = temp;
    			i++;
    		}
    	}
    I would like to make the array dynamic though...

    \Esben.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I would like to make the array dynamic though
    How dynamic?

    Does the file contain the "row" and "col" values as the first two data items?

    Or do we figure out how many rows and cols there are from just reading how many values there are on each line, and how many lines there are?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    >>Salem,

    How dynamic... good point...

    What I would like to do is to return the DataMatrix[][] variable from one function, back to an int pointer (int *p) in the calling function. Is this possible in any way or need I return it to an integer-array pointer( int (*p)[]). I am not very happy about the latter solution...

    Hope this clearified my problem a bit.

    \Esben.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well assuming that the first two numbers are the dimentions...

    Code:
    int **DataMatrix;
    fscanf(InFile, "%d %d", &row, &col);
    DataMatrix = malloc ( row * sizeof *DataMatrix );
    for ( i = 0 ; i < row ; i++ ) {
      DataMatrix[i] = malloc ( col * sizeof *DataMatrix[i] );
    }
    You then have your file reading code as it stands, then finally you
    return DataMatrix;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *mutters something about a search button and wanders off*


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Store the height, width and size in the file header.

    Read in the file header using good old C I/O with _open, _read, etc.

    Allocate memory for the array based on the info in the header.

    Read in the data from the disk into the array using _read.

    Close the file.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Thanks for your replies guys! All helpfull.

    \Esben.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  3. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM