GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Mar-2005, 12:13
robin1977 robin1977 is offline
New Member
 
Join Date: Mar 2005
Posts: 2
robin1977 is on a distinguished road
Question

Urgent !question about read file(variable length)


I want to read a matrix from a file. First is the degree of matrix, then the binary matrix,eg:

4
0001
1110
0010
0011
5
00011
00111
10000
01100
11100
...

I need to use each matrix to construct adjacency linklist. So I need to first read each matrix to and temp[] array, but it seems that the length of the array is not certain.if I used
fscanf("%d...%d",temp[1],...), I dont know how to write out,because the ,legnth is variable.

Any expert can give me a solution?
Thanks a lot
  #2  
Old 13-Mar-2005, 16:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by robin1977
I want to read a matrix from a file. First is the degree of matrix, then the binary matrix,eg:

4
0001
1110
0010
0011


Here's a complete solution for dynamically allocated 2D arrays. I put in values for the number of rows and columns instead of getting user inputs; try your own values in this test program, then put the allocation routines (or something equivalent) into your programs. You can make any size matrix (limited only by memory that your system and your compiler can handle). The matrices don't have to be square; try different values for number of rows and number of columns.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
  /* imatrix is a pointer to a pointer to an int */
  
  int **imatrix;

  int numrows;
  int numcols;
  int row, col;

  numrows = 4;
  numcols = 4;

  /*
   * Allocate space for pointers to the rows
   *  The pointer to row 0 is imatrix[0]
   *  The pointer to row 1 is imatrix[1]
   *  etc.
   *
   * Always check return value from malloc,
   * and always call free() for everything
   * that malloc() gives you (after you are
   * through with it, of course).
   *
   */

  imatrix = malloc(numrows * sizeof(int *));
  if (!imatrix) {
    printf("Error allocating %d bytes for imatrix\n", numrows * sizeof(int *));
    return 0;
  }
  else { /* you don't really need to print this, but just to see: */
    printf("Allocated %d bytes for imatrix\n", numrows * sizeof(int *));
  }

  /* 
   * Now, for each row, allocate space for
   * the matrix columns
   * the pointer to the first  column of row 0 is imatrix[0][0]
   * The pointer to the second column of row 0 is imatrix[0][1]
   * etc
   *
   */


  for (row = 0; row < numrows; row++) {
    imatrix[row] = malloc(numcols * sizeof(int));
    if (!imatrix[row]) {
      printf("Error allocating %d bytes for imatrix[%d]\n", 
              numcols * sizeof(int), row);
      for (; row-1>= 0; row--) {
        free(imatrix[row]);
      }
      free(imatrix);
      return 0;
    }
    else { /* you don't really need to print this, but just to see: */
      printf("Allocated %d bytes for row number %d\n", 
              numcols * sizeof(int), row);
    }
  }

  /* 
   * Here is a loop that stores different values in each element
   */
  for (row = 0; row < numrows; row++) {
    for (col = 0; col < numcols; col++) {
      imatrix[row][col] = row * numcols + col;
    }
  }

  /*
   * Now, print out the matrix
   */
  for (row = 0; row < numrows; row++) {
    for (col = 0; col < numcols; col++) {
      printf("matrix[%d][%d] = %d\n", row, col, imatrix[row][col]);
    }
  }
  return 0;

  /* now free up everything that malloc() gave you */
  for (row = 0; row < numrows; row++) {
    free(imatrix[row]);
  }
  free(imatrix);

}

Regards,

Dave
  #3  
Old 13-Mar-2005, 18:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by robin1977
I want to read a matrix from a file. First is the degree of matrix, then the binary matrix,eg:

4
0001
1110
0010
0011

I need to use each matrix to construct adjacency linklist. So I need to first read each matrix to and temp[] array, but it seems that the length of the array is not certain.if I used
fscanf("%d...%d",temp[1],...), I dont know how to write out,because the ,legnth is variable.

Any expert can give me a solution?
Thanks a lot

My previous post gave a way to get the data structure (matrix) to handle your problem, but I forgot to suggest a way to read in the values.

I suggest that you don't use fscanf(). Read each line into a buffer (char array) with fgets(), then use a loop to read a character at a time from the buffer into the matrix elements (you can read in '0' and '1' chars and convert to integer values 0 and 1 before storing if you want to).

Regards,

Dave

p.s.
This and the previous post were "C" approaches. C++ programmers would use the getline() function for their istream instead of fgets() as I did here. They might also use new and delete instead of malloc() and free() as I did in my previous post, but actually dynamically allocated matrices would probably be implemented as vectors of vectors (no new, no free).
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Having a problem Chuckles Computer Hardware Forum 19 13-Sep-2004 13:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.