![]() |
|
#1
|
|||
|
|||
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
|
|||
|
|||
|
Quote:
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:
Regards, Dave |
|
#3
|
|||
|
|||
|
Quote:
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 GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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