OK - thanks folks I now have the data into two arrays! nadroj - you mentioned it to be bad practice using feof() to control a loop, and I can now see for why as the last value stored into the array is '0.000000'
With being a novice I'm a little unsure how I would go about improving the code? Below is the code as it currently stands:
Code:
/*
* File: newmain.c
* Author: paul
*
* Created on 09 February 2010, 19:37
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main() {
FILE *file;
float col1[100];
float col2[100];
float curr_arr;
/* make sure it is large enough to hold all the data! */
int i, j;
file = fopen("/home/paul/short.txt", "r");
if (file == NULL) {
printf("Error: can't open file.\n");
return 1;
} else {
printf("File opened successfully.\n");
i = 0;
while (!feof(file)) {
/* loop through and store the numbers into the array */
fscanf(file, "%f\t%f", &col1[i], &col2[i]);
i++;
}
}
printf("Number of numbers read: %d\n\n", i);
printf("The numbers are:\n");
printf("\nColumn 1:\n\n");
for (j = 0; j < i; j++) { /* now print them out 1 by 1 */
printf("%f\n", col1[j]);
}
printf("\nColumn 2:\n\n");
for (j = 0; j < i; j++) {
printf("%f\n", col2[j]);
}
fclose(file);
return 0;
}