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 02-Jun-2005, 00:00
amina17 amina17 is offline
New Member
 
Join Date: Jun 2005
Posts: 3
amina17 is on a distinguished road
Unhappy

saving matrix from file to array


I 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  
Old 02-Jun-2005, 09:12
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Post the code you have, and post a sample of the input file.
  #3  
Old 02-Jun-2005, 13:14
amina17 amina17 is offline
New Member
 
Join Date: Jun 2005
Posts: 3
amina17 is on a distinguished road
Quote:
Originally Posted by Dave Sinkula
Post the code you have, and post a sample of the input file.

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:
);
					printf("\n");
				}
			} /* closing first else */


	  getch();

	  /* close the data file */
	  fclose(fp);

} /* closing main */
Last edited by LuciWiz : 03-Jun-2005 at 01:27. Reason: Please insert your C code between [c] & [/c] tags
  #4  
Old 02-Jun-2005, 13:33
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Is there always 10x10 data? If so, I'd go with something like this.

CPP / C++ / C Code:
#include <stdio.h>

int main(void)
{
   static const char filename[] = "file.txt";
   FILE *file = fopen(filename, "r");
   if ( file )
   {
      int array[10][10];
      size_t i, j;
      for ( i = 0; i < sizeof array / sizeof *array; ++i )
      {
         for ( j = 0; j < sizeof array / sizeof *array; ++j )
         {
            if ( fscanf(file, "%d", &array[i][j]) != 1 )
            {
               puts("error in file");
               return 0;
            }
         }
      }
      fclose(file);
      for ( i = 0; i < sizeof array / sizeof *array; ++i )
      {
         for ( j = 0; j < sizeof array / sizeof *array; ++j )
         {
            printf("%4d", array[i][j]);
         }
         putchar('\n');
      }
   }
   else
   {
      perror(filename);
   }
   return 0;
}

/* file.txt
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
*/

/* my output
   1   2   3   4   5   6   7   8   9  10
  11  12  13  14  15  16  17  18  19  20
  21  22  23  24  25  26  27  28  29  30
  31  32  33  34  35  36  37  38  39  40
  41  42  43  44  45  46  47  48  49  50
  51  52  53  54  55  56  57  58  59  60
  61  62  63  64  65  66  67  68  69  70
  71  72  73  74  75  76  77  78  79  80
  81  82  83  84  85  86  87  88  89  90
  91  92  93  94  95  96  97  98  99 100
*/
  #5  
Old 03-Jun-2005, 12:05
amina17 amina17 is offline
New Member
 
Join Date: Jun 2005
Posts: 3
amina17 is on a distinguished road
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  
Old 03-Jun-2005, 13:04
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by amina17
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... !!
Any dimensions at runtime, or at compile time? C or C++?

Quote:
Originally Posted by amina17
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....
It works, as shown, with the input shown. Is your file text or binary?

Quote:
Originally Posted by amina17
n if neone else knos some way to copy matrices ( containing single n multiple values) into arrays.. plz let me kno!!
Let's take this one step at a time.
 
 

Recent GIDBlogPython ebook 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
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

All times are GMT -6. The time now is 07:03.


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