Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[C]Reading matrix from csv file in C

csv csv file matrix reading

Best Answer 0xDEADBEEF, 19 January 2015 - 01:49 PM

Hmm I think I see what you are attempting todo. You think the first scanf will find all ',' and return you the count. And that the second will find all the newlines and return you the count.

 

But that's not how the format specifiers work...

 

You'll have todo it more manually (you could scan through each character counting the ',' to get the row count, as well as counting the '\n') or there are a few other methods, i'd abandon trying to get scanf todo everything for you.

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 Ritwik I the programmer

Ritwik I the programmer

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 19 January 2015 - 04:29 AM

Our professor asked us to write a function to read a matrix from a csv file in c without knowing number of rows and collumns in it, and store the matrix in dynamically allocated memory. Could someone point me to where I am wrong? I think I may have messed up the scanf format for ignoring and reading specific characters. If so, please correct me.

double** fscanmat(const char* fname){
	FILE *fp = fopen(fname, "r");
	int r, c, i, j;
	r = scanf("%[,][^\n]");
	fseek(fp, 0, SEEK_SET);
	c = scanf("%[\n]");
	fseek(fp, 0, SEEK_SET);
	double **mat = (double**)(malloc(r * sizeof(double*)));
	for(i = 0; i < r; i++)
		mat[i] = (double*)(malloc(c * sizeof(double)));
	for(i = 0; i < r; i++)
		for(j = 0; j < c; j++)
			scanf("%lf[^,\n]", (*(mat+i)+ j));
	return mat;
}

I can believe, but why should I?


#2 Ritwik I the programmer

Ritwik I the programmer

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 19 January 2015 - 07:52 AM

Revised code - still doesn't work:

#include <stdio.h>
#include <stdlib.h>
double** fscanmat(char* fname){
	FILE *fp = fopen(fname, "r");
	char dummy[10];
	int r, c, i, j;
	r = fscanf(fp, "%[,^\n]n");
	fseek(fp, 0, SEEK_SET);
	c = fscanf(fp, "%[\n]n");
	printf("%d %d", r, c);
	fseek(fp, 0, SEEK_SET);
	double **mat = (double**)(malloc(r * sizeof(double*)));
	for(i = 0; i < r; i++)
		mat[i] = (double*)(malloc(c * sizeof(double)));
	for(i = 0; i < r; i++)
		for(j = 0; j < c; j++)
			fscanf(fp, "%[^,\n]lf", (*(mat+i)+ j));
	fclose(fp);
	return mat;
}

I am pretty sure there's nothing wrong in the allocation part, but the debug statement shows both r and c to be zero, so essentially, nothing is being allocated. There is a slightly lengthier way involving character by character analysis which works, but that is a little tedious. Also note that size of array HAS to be read by couting commas and newlines as per specifications of format given in the assignment.


Edited by Ritwik I the programmer, 19 January 2015 - 08:38 AM.

I can believe, but why should I?


#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 19 January 2015 - 09:09 AM

What is the file format? You said it's a CSV, but you appear to be looking for brackets.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts

Posted 19 January 2015 - 12:37 PM

yep having the format of the file would be helpful.


Creating SEGFAULTs since 1995.


#5 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts

Posted 19 January 2015 - 01:49 PM   Best Answer

Hmm I think I see what you are attempting todo. You think the first scanf will find all ',' and return you the count. And that the second will find all the newlines and return you the count.

 

But that's not how the format specifiers work...

 

You'll have todo it more manually (you could scan through each character counting the ',' to get the row count, as well as counting the '\n') or there are a few other methods, i'd abandon trying to get scanf todo everything for you.


Creating SEGFAULTs since 1995.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download