hi......I'm terribly new to programming and have been trying to figure out the proper command to no avail. My input data is something like the following:
apple bee c .....
1.2 2.3 0 .....
2.4 3.5 3.2 ....
.... ..... .....
So the first line are strings that are 'labels' for the columns of numerical data which I want to store in an array. The numerical data I want to store in an matrix. One of my problems is that before opening the file I don't know how many rows or columns the data will have.
This is all I've come up with at the moment. I think I need to use fscanf somehow......but have been very confused by how to use that. Thanks in advance for any help.
I apologize in advance for my poor attempt at writing the code for this part. If anybody could help at all with how to read in a line and store the value into an array that would be greatly appreciated.Code:FILE *file; file = fopen("data/hello.txt", "r"); if(file==NULL) { printf("Error: can't open file.\n"); return 1; } else { printf("File opened. Now read file\n"); //read first line as string and process to count number of columns, then read all the lines following that to get number of rows of the data until eof //use malloc to create matrix of double of size row x column (I have a function for this elsewhere) //start at the beginning of the file and loop through to read each line and somehow store into array[row][col] fclose(file);



LinkBack URL
About LinkBacks



(tells you how many columns there has to be in your matrix).