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.