Storing # from Data File in Arrays?
Hi I was wondering if it was possible to store a data file in an array:
The data file (vec_in.dat) would look like this :
Code:
1.2 -2.3 0.53 2.01 -1.5
I'm trying to store these 5 numbers in an array called v[5]. I want to manipulate the data in v[5] and spit out a product stored in vec[5]. Then I want to print the output onto a screen and a data file.
This is my code so far:
Code:
#include <stdio.h>
#include <math.h>
int main(){
int k;
double text[81], v[5], vec[5];
double norm, mid;
FILE *Inf_1, *Outf_1;
Inf_1 = fopen("vec_in.dat", "r");
Outf_1 = fopen("vec_out.dat", "w");
if ((Inf_1 == NULL) || (Outf_1 == NULL))
{
printf("Error oepning the file\n");
exit(1);
}
while(fgets(text,81,Inf_1)!=NULL)
{
sscanf(text, "%lf", &v);
for (k=0; k<=5; k++) {
mid += pow(v[k],2);
}
norm = sqrt(mid);
for (k=0; k<=5;k++) {
vec[k] = v[k]/norm;
}
}
printf("\nOriginal Vector = [");
printf("%.6f", v[k]);
printf(" ]\n");
printf("\nNormalized Vector = [");
printf("%.6f", vec[k]);
printf(" ]\n");
fprintf(Outf_1,"\nOriginal Vector = [");
fprintf(Outf_1,"%.6f", v[k]);
fprintf(Outf_1," ]\n");
fprintf(Outf_1,"\nNormalized Vector = [");
fprintf(Outf_1,"%.6f", vec[k]);
fprintf(Outf_1,"\n");
fclose(Inf_1);
fclose(Outf_1);
exit(0);
}
Currently, I'm getting these errors:
15 q3.c
warning: passing arg 1 of `fgets' from incompatible pointer type
17 q3.c
warning: passing arg 1 of `sscanf' from incompatible pointer type
Am I using the fgets correctly? Is v[k] being properly converted to vec[k]?