So here's my situation: I have a matrix with numbers that have 3 decimal point precision and potentially any amount of numbers before the decimal point. The matrix is printed on a file in the following format.
Matrix has x rows and y columns:
1.333 2.666 4.222
3.999 5.547 4.318

Now let me break down the problems.

Problem 1: getting the number of rows and columns without getting to the next line.

#include <stdio.h>

int main (){
	FILE *file_to_be_read; /*File has to be capslocked*/
	int c;
	int x;
	file_to_be_read = fopen("TestData.txt", "r");
	while ((c = getc(file_to_be_read)) != '\n'){
		if ((isalpha(c) == 0)){
			x = c;
		}
	}
	printf("%i" , x);
	return 0;
}

So I made a small bit of code to read the numbers from a giving string of text and the text looks like this.

I have 1 fishy
There are 3 phat bunnies

Unfortunately when the program is run, I get the following. 13 which means the program read past the first line which it shouldn't have due to the != '\n'. What is wrong and how am I supposed to fix it?

Problem 2: How to retrieve the numbers from the string so that they can be individually stored in a 2D pointer array?

Well I have done my research on this topic for a while and its been a bit frustrating. A lot of the number searching in string searches that I found indicated that the numbers would be of a fixed length which isn't the case in this scenario and when I tried to use fgetc to detect spaces with something like = ' ', it didn't work. So is there any way for fgetc to detect spaces and how would you do that, and if not, is there any other way to get the numbers from the file?

Thanks for taking your time to read this lengthy thing.

why not just call fscanf() to read in those numbers

float number;
while( fscanf(file_to_be_read, "%f", &number) > 0)
{
   // do something with this number, such as put it into an array of floats
}

Edited 6 Years Ago by Ancient Dragon: n/a

From what little I understand of scanf it appears that it reads from stdin (and I read from file) and also, I don't exactly know how many columns will be there. When I looked at scanf I think I see it looking for a specific amount of integers or words or whatever, but the problem with that is that I don't know how many integers there are. Maybe I don't understand scanf, but from what I have read from cplusplus.com and elsewhere I think sscanf would work but even then I am not sure. Could you show some pseducode on how to read the numbers from the file or explain scanf? Thanks.

scanf() reads from stdin, fscanf() which I suggedted reads from the file. You need to learn to read a little better.

>>I don't exactly know how many columns will be there
doesn't matter. fscanf() will convert everything up to the first non-numeric character (for %f that includes the decimal point if there is one).

>>Could you show some pseducode on how to read the numbers from the file or explain scanf?
I already did that. Just read my previous post.

Many times it is educational to write and test small programs that do what you don't understand. For example, take the code I posted and put it in a C program all by itself, compile it, and run it. Of course you will have to add the little bit of code that you posted which declares the FILE* and opens the file. Now inside the while loop just replace that comment I made with a printf() statement which just dispays the value of the float variable. When you do that you will understand better how fscanf() works.

Edited 6 Years Ago by Ancient Dragon: n/a

Sorry about that, I was in a rush to class and didn't read your post properly. I went and tried out fscanf in a small program and it didn't work.

#include <stdio.h>

int main (){
	FILE *file_to_be_read; /*File has to be capslocked*/
	file_to_be_read = fopen("TestData.txt", "r");
	double n;
	while (fscanf(file_to_be_read, "%Lf", &n) > 0){
		printf("%s", "This worked at least once . . . ");
		printf("%f", n);
	}
	return 0;
}

So the TestData.txt has the following text.

I have 32.489 fishy
52.366 42.771 62.999
51.550 41.011 77.777

And it prints out nothing. I just know that the fscanf has not picked up any numbers and the output remains essentially blank. I think it may have to do with the non number characters within the text file but from what I have read I can only assume fscanf has skipped them and that the numbers in the file are of a different format.

This article has been dead for over six months. Start a new discussion instead.