C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-26-2008, 01:47 PM   #1
Registered User
 
Join Date: Feb 2008
Posts: 16
Unhappy Need Help: Storing numeric data into matrix from a data file

Hi all,

I need your help regarding huge file handling. Suppose i want to read a file and store numeric data into 2D matrix. The file i am using is attached (a .dat file converted into .txt for this post) and it contains 690 rows and 15 columns.

I have written a sample program that asks user to provide the name of the file in the "main" function and calls another function "getdata" which counts number of rows and columns (though it is provided but i need this for my future work), stores numeric data into a 2D matrix and returns a structure pointer (contains rowCount, columnCount and a 2D array pointer).

My code works for small data but somehow fails for the datafile attached with this thread.

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;
	char 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');
		}
	 return 0;
	 }
Please help.
Thanks.
Attached Files
File Type: txt data.txt (28.7 KB, 15 views)
spiit231 is offline   Reply With Quote
Old 02-26-2008, 02:02 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Your second call to fopen doesn't use the same file as the first. (You shouldn't close-and-open anyway; use rewind() instead.)

You don't check that fopen actually opens a file.

gets() is bad; look into fgets instead. (Not an immediate issue, but soon.)
tabstop is offline   Reply With Quote
Old 02-26-2008, 02:02 PM   #3
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
do not use gets - see FAQ
use fgets - see FAQ how: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

fgetc returns int - declare ch as int
increase your warning level to avoid such errors

check the return value of all functions tha can fail like fopen, malloc, fscanf

do not cast malloc - see FAQ
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 02-26-2008, 02:12 PM   #4
Registered User
 
Join Date: Feb 2008
Posts: 16
Oh... i am sorry. Actually I used fgets but somehow during i changed it to gets. But my problem remains in both cases.
spiit231 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Structure Eror prominababy C Programming 3 01-06-2009 09:35 AM
Abnormal Program Termination when executed from C:\Program Files\... m37h0d Windows Programming 48 09-26-2008 03:45 AM
Skipping records to text file and storing data. Mr. Deeds C Programming 5 09-10-2007 11:24 PM
Possible circular definition with singleton objects techrolla C++ Programming 3 12-26-2004 10:46 AM
HUGE fps jump DavidP Game Programming 23 07-01-2004 10:36 AM


All times are GMT -6. The time now is 04:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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