I am trying to run a simple C program that takes a file of random floating point values, automatically identify the length of the file and use the length to perform further computation. However, my compiler either hangs or I get erroneous results. Here is my code
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
int main()
{
FILE *fptr;
int count = 0; // Line counter (result)
char ch; // To store a character read from file
if ((fptr = fopen("C:\\Users\\Evandovich\\Desktop\\White_Noise.txt","r")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
// Extract characters from file and store in character ch
for (ch = getc(fptr); ch != EOF; ch = getc(fptr))
{
if (ch == '\n') // Increment count if this character is newline
count = count + 1;
}
printf("The file has %d lines\n ", count);
// use the value of "count" to be the length of the array.
char arrayNum[count];
char *eptr;
double result, result1[count];
for (int i = 0; i<count; i++)
{
fscanf(fptr,"%s", &arrayNum[i]);
/* Convert the provided value to a double */
result = strtod(&arrayNum[i], &eptr);
result1[i] = pow(result,2) ;
printf("value %f\n", result1[i]);
}
fclose(fptr);
return 0;
}
What particularly is the error? Your input is well appreciated
INPUT file (N.txt) contains
0.137726
0.390126
-0.883234
0.006154
-0.170388
-1.651212
0.510328
OUTPUT The file has 7 files
value 0.000000
value 0.000000
value 0.000000
value 0.000000
value 0.000000
value 0.000000
value 0.000000
Expected The file has 7 files
value 0.018968
value 0.152198
value 0.780102
value 0.000038
value 0.029032
value 2.726501
value 0.260435
Aucun commentaire:
Enregistrer un commentaire