Link to home
Start Free TrialLog in
Avatar of Naverick
Naverick

asked on

Read file from buffer into array

Hi
I have written a code that reads a file in two passes
In the first pass it counts the number of rows and colum to allocate memory for a 2d array.
After rewind, in the second pass it reads each char of the file and puts them in the array.

The size of the array can varry but is approx( 1000 plus x 360). While the size of each file should be well less than 1MB.

As i need to process several files i was told i could use the buffer  to put it in the array instead of  pasing through the file the second time  as this could possibly reduce computing time.

However, i have no idea on how to modify the code to read form buffer and copy into the dynamically created array.  Any help would me much appreciated. Thx

Ps. i have attached a samlpe text file of small dimensions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
 
int main(int argc, char *argv[])
	{
	FILE *fin, *fout ; 
	int colCnt,rowCnt, col=0,row=0;
	int i,j;
	int**matrix;
	
 
	if(argc!=2)
		{
		printf("Check Arguments\n");
		exit(0);
		}
	if(!(fin=fopen(argv[1],"r")))
		{
		printf("Unable to open file\n");
		exit(1);
		}
	
	while((colCnt=fgetc(fin))!='\n')
		{
		if(colCnt=='\t')
			{
			++col;
			}
		}
	
	row++;
	while((rowCnt=fgetc(fin))!=EOF)
		{
		if(rowCnt=='\n')
			{
			++row;
			}
		}
 
	rewind(fin);
	
	printf("Number of cols = %d ",col);
	printf("Number of rows = %d\n ",row);
 
	matrix =(int**)malloc(row*sizeof(int*));
	if (matrix == NULL)  
    {
        fprintf(stderr, "ERROR - malloc failed: Exiting Program!\n\n");
        exit(EXIT_FAILURE);
    }
 
	for(i=0;i<row;i++)
		{
		matrix[i]=(int*)malloc(col*sizeof(int));
		}
 
	for(i=0;i<row;i++)
		{
		for(j=0;j<col;j++)
			{
			fscanf(fin,"%d",&matrix[i][j]);
			printf("%d ",matrix[i][j]);
			}
		printf("\n");
		}
	fclose(fin);
	free(matrix);
	return 0;		
	}

Open in new window

hist.txt
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Sorry... just a slightly off topic response but I thought it might help... a good general purpose allocation strategy is to allocate memory using a power of 2 (2, 4, 8 16, 32 etc). This is a strategy that is very simple to implement and good for general purpose use. You could, rather than implement a buffer, just use realloc to resize your matrix using this strategy. If the matrix is a jagged array (not all rows need to be the same length) even better since you can grow each row as and when needed.

Use this suggestion as you will.. or not :)
SOLUTION
Avatar of ahao
ahao

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Naverick
Naverick

ASKER

with respect to your comment...im not sure of the exact method, format annd syntax to allocate a large enough array and then resize it after  all data has been entered into it. All i know is malloc and realloc has to be used.
just seen it...will try it out....but please can u explain to me what is iit that u have done..cos im not too good at this and i would like to understand the code. Thx a lot.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ive tried it and offcourse u know it works better than i know. A very frief algorithim for understanding would be great. Thanks a lot. While u r here can i ask you another question.

How can i dynamically create an array and then keep adding rows to it in a loop till required.
Or/and
How can i create a large array an someone mentioned above and later resize it to a smaller one or in other words remove rows.
You can use realloc to resize the buffer.
For simplicity, I use one-dimensional array.


	int size = 5;
 
	int * p = (int*)malloc( size * sizeof(int) );
	// now the array size is 5
 
	// add one
	p = (int*)realloc( p, 6 * sizeof(int) );
	// now the array size is 6
 
	// remove one
	p = (int*)realloc( p, 4 * sizeof(int) );
	// now the array size is 4
 
	free( p );

Open in new window

thatx a lot....will get back  to u when i get stuck tryin to implement this