![]() |
|
#1
|
|||
|
|||
saving matrix from file to arrayI need to read a matrix..(2D dynamically allocated array ) from file.. display... i'm doing this using fscanf and printing to console...
problem arises while trying to store this matrix to array!! could someone please tell me the syntax to directly store elements read from file to array co-ordinates i've tried eveything... my array just prints garbage values thanks |
|
#2
|
|||
|
|||
|
Post the code you have, and post a sample of the input file.
|
|
#3
|
|||
|
|||
|
Quote:
Hi.. im pasting a copy of the code below.. im actually doing my MS in bioinformatics... n this a part of my project work.. im trying to make an application n trying to develop segments of my code... input can be any text file with a matrix (right now with binary data... only format specification to be taken care of is "space" b/w columns and "newline" for next row) thx.. im seriously harrowed with this code! [c] /* PROGRAM TO READ BINARY DATA FROM FILE AND COMPUTE DISTANCE MATRIX */ #include<stdio.h> #include<conio.h> #define NULL 0 /* defining max dimensions of array = 10x10 = 100 elements */ #define MAXROWS 10 #define MAXCOLS 10 void main() { FILE *fp; /* define a pointer to a predefined structure type FILE */ char filename[10]; char number,val; int row= -1,col= 1; /* counter to count row and col */ int i= -1,j= 1; /* counter to increment array co-ordinates while storing values */ int r= -0,c= 0; /* counter to print matrix */ /* Array definition */ int matrix[MAXROWS][MAXCOLS]; clrscr(); printf("\n\t\t PROGRAM TO COMPUTE DISTANCE MATRIX \n"); printf("\n Please enter filename:"); scanf("%s",filename); /* open an existing file in read only mode */ if ((fp = fopen(filename,"r")) == NULL) { printf("\n ERROR: cannot open file"); } else /* read data from file and print on screen */ { printf("\n Reading matrix from file:\n\n"); if(feof(fp)) { printf("\n end of file \n"); } /* loop structure for counting rows and cols of matrix in file */ while (!feof(fp)) { fscanf(fp,"%c",&number); /*scanning for ascii value of 0 and 1 respectively*/ if ( number == 48 || number == 49) { fprintf(stdout,"%c",number); matrix[i][j]=number; } /* ascii value of space = 32.. print to console.. increment column*/ else if(number == 32) { matrix[i][j]=number; fprintf(stdout,"%c",number); while(val!= 10) /*while not newline*/ { col++; j++; break; } } /*ascii value of newline = 10...print to console..increment row*/ else if ( number == 10) { fprintf(stdout,"\n"); val = 10; row++; i++; } }/* end of while eof */ printf("\nNumber of rows = %d\nNumber of cols = %d",row,col); printf("\nvalue of i=%d\nvalue of j=%d",i,j); /* printing matrix from array */ printf("\n\nPrinting matrix stored in array: \n\n"); for(r=0;r<=i;r++) { for(c=0;c<=j;c++) printf("%d",matrix[r] CPP / C++ / C Code:
Last edited by LuciWiz : 03-Jun-2005 at 01:27.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#4
|
|||
|
|||
|
Is there always 10x10 data? If so, I'd go with something like this.
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
|
hey dave.. thx for helpin me out...
i dnt think im being very clear with wht im tryin to do.... 1) im just settin max matrix elements to 100 ( at this stage) ... wht i want the code to do is.... read the file for any dimension matrix and count rows n cols... as u'll can see... i dnt hav a very efficient code for tht... !! 2) the fscanf statement u mentioned... does'nt work! tried all file functions... fputs fread... everything just works to read n copy string!! i tried ur code... n in my code too... matrix just has zeros at this stage... so it is'nt storing values.... the code i posted is just a rough draft... let me kno if i could email u my original code or something... n if neone else knos some way to copy matrices ( containing single n multiple values) into arrays.. plz let me kno!! |
|
#6
|
|||
|
|||
|
Quote:
Quote:
Quote:
|
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 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 22:26 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 11:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The