- #1
vuongstran
- 1
- 0
Homework Statement
Well the program has several functions but I just really need help with reading a text file and splitting the data in the file into a one-dimensional array and a two-dimensional array
The text file is like this...
21
1110 1.5 5.4 6.0 9.8. 8.5 8.3 5.6 9.9
1112 1.5 5.4 6.0 9.8. 8.5 8.3 5.6 9.9
I have to somehow get the pin (1110 & 1112) into a 1-d array while putting the rest of the numbers into a 2-d array.
This is what I have so far.
/* =========================================================== */
/* Reads data of a file and places the first column of info
into a one dimensional array and the rest into a
table.
PRE: table = empty
ary = empty
POST:
*/
int readTable (double scores[][MAX_COLS], double diverId[], int lineSize, char fileName[])
{
// Local Declarations
FILE *fpScores;
int r, c;
// Statements
fpScores = fopen( "scores.txt", "r" );
int numOfDivers = 0;
fscanf(fpScores, "%d", &numOfDivers);
printf("The numbers of divers is %d.\n\n", numOfDivers);
for (r = 0; r < MAX_ROWS; r++)
printf("PIN DIF J1 J2 J3 J4 J5 J6 J7\n");
printf("=== === == == == == == == ==\n");
for (r = 0; r < MAX_ROWS; r++)
{
fscanf(fpScores, "%lf", &diverId[r]);
printf("%.1lf ", diverId[r]);
if( ( r + 1 ) % lineSize == 0 )
printf( "\n");
}
printf("\n");
...end of function
The problem is that the for loop goes through the file and makes every value have its own row like this:
1111
1.5
5.4
6.0
etc..
Any help with be appreciated. Thanks!