Need simple code for large file handling

This is a discussion on Need simple code for large file handling within the C Programming forums, part of the General Programming Boards category; Hi all, I need a simple code for storing numeric data into a 2D matrix (consider dimensions are unknown) from ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    16

    Need simple code for large file handling

    Hi all,

    I need a simple code for storing numeric data into a 2D matrix (consider dimensions are unknown) from a data-numeric format file.

    The file is uploaded here...

    http://archive.ics.uci.edu/ml/machin...n.data-numeric

    I have tried but somehow failed.

    Need your cooperation,

    thanks.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    somehow failed
    is very descriptive bug report
    Read this: http://cboard.cprogramming.com/annou...t.php?f=4&a=39

    post your last code
    describe your problem (what you expect, what you get, how it is different)
    ask for directions
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    16
    I'm extremely sorry. I have forgot to post my code. Here it is...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct matrix
    {
    	int nRow;
    	int nCol;
    	float **m;
    };
    
    typedef struct matrix *data;
    
    data getdata(char file_path_name[])
    {
    	FILE *db;
    	int ch;
    	int i, j, cols;
    	int rows = 0, entries = 0;
    
    	data ptrdata=(data)malloc(sizeof(data*)) ;
    	db = fopen(file_path_name, "r");
    
    	printf("\nCollecting information...\n");
    
    	while(1)
    		{
    			ch=fgetc(db);
    			if(ch==EOF)
    				{
    					//rows++;
    					//entries++;
    					break;
    					}
    			if(ch=='\n')
    				{
    					rows++;
    					entries++;
    					}
    			if(ch==' ')
    			entries++;
    			}
    
    	fclose(db);
    
    	cols = entries/rows;
    
    	printf("\nData contains %d rows and %d columns\n", rows, cols);
    
    	ptrdata->nRow = rows;
    	ptrdata->nCol = cols;
    
    	db = fopen("data.dat", "r");
    	ptrdata->m = (float **) malloc(rows * sizeof(float *));
    
    	for(i = 0; i < rows; i++)
    		ptrdata->m[i] = (float *) malloc(cols * sizeof(float));
    
    	for(i = 0; i < rows; i++)
    		for(j = 0; j < cols; j++)
    			fscanf(db, "%f", &(ptrdata->m[i][j]));
    
    	fclose(db);
    	return ptrdata;
    }
    
    int main()
    {
    	 char file_path_name[100];
    	 data pb;
    	 int i, j;
    
    	 printf("Provide name of the file (including path):\n");
    	 gets(file_path_name); //data.dat
    
    	 pb = getdata(file_path_name);
    
    	 printf("\nPrinting data matrix...\n\n");
    
    	 for(i = 0; i < pb->nRow; i++)
    	 //for(i = 0; i < 5; i++)
    	 {
    		for(j = 0; j < pb->nCol; j++)
    		//for(j = 0; j < 5; j++)
    		{
    			printf("%.2f ",*(*((pb->m)+i)+j));
    			}
    		putchar('\n');
    		}
    	 free(pb);
    	 return 0;
    	 }

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You still haven't said "what you expect, what you get, how it is different" -- which was good advice. As such, I'll have to guess.

    Some general guidelines:
    • Don't use gets(), it's dangerous. cpwiki.sf.net/Gets
    • You should free() your malloc()'d memory when you're done with it. (All dimensions.)


    As for your problem:
    Code:
    	for(i = 0; i < rows; i++)
    		for(j = 0; j < cols; j++)
    			fscanf(db, "&#37;f", &(ptrdata->m[i][j]));
    Try switching those for loops around . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
    	db = fopen(file_path_name, "r");
    
    	db = fopen("data.dat", "r");
    you still have not fixed this
    as well as other notes made in the previos thread
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21