Read in float values from two columns
Hello All,
I'm having trouble getting this code to work. Basically, I have been given a '.txt' file with two columns of extensive float values. We have been asked to write a program that will separate the columns into two arrays in order to perform various calculations on them later.
I have tried to create a while loop that runs through and alternates between the arrays causing 'fscanf' to insert data into each of the alternating arrays as it passes through. My main problem is within the 'if' statements. I don't know how to perform a comparison of variable names rather than the variable contents. I'm sure you'll notice what I've tried to achieve when looking at this code:
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;
curr_arr = col1[0];
while (!feof(file)) {
/* loop through and store the numbers into the array */
fscanf(file, "%f", &curr_arr);
if (&curr_arr == &col1[i]) {
curr_arr = col2[i];
} else if (&curr_arr == &col2[i]) {
curr_arr = col1[i];
} else {
printf("An error ocurred.\n");
return 1;
}
i++;
}
}
printf("Number of numbers read: %d\n\n", i);
printf("The numbers are:\n");
for (j = 0; j < i; j++) { /* now print them out 1 by 1 */
printf("%f\n", col1[j]);
printf("%f\n", col2[j]);
}
fclose(file);
return 0;
}
I'll be very grateful for any help you may offer on the matter!
Kind Regards,
Paul S.