0

The text file contains 52 lines that are in the format:

A .013420
B .000191
C .011222
...

I want to ignore the letters and I need to extract the values from the file and store the first 26 in one array which I named freqOne[] and store the last 26 values in another array named freqTwo[]. I will later use these values for calculations. here is my attempt:

#include <stdio.h>
#include <stdlib.h>

int main (){

   FILE *input1;
   /*char freqOne[26]; i use these arrays for attempt 1 
     char freqTwo[26];*/
   double freqOne[26];
   double freqTwo[26];


   input1 = fopen("test8.txt", "r");
   if(input1 == NULL){
      perror("test8.txt");
      exit(EXIT_FAILURE);
   }

   /* attempt one: all the values print out correctly but idk how to use them :(*/

   /*while(fgets(freqOne, sizeof(freqOne), input1)){
     printf("%s", freqOne);
     }
     while(fgets(freqTwo, sizeof(freqTwo), input1)){
     printf("%s", freqTwo);
     }
    */

   /*fclose(input1);    */

   int h;
   int i;
   /* another attempt i made, this one prints out the a large negative number for every element :(*/
   for(i=0; i<26; i++){
      fscanf(input1,"%lf", &freqOne[i]);
      printf("%lf\n", freqOne[i]);

   }
   for(h=0;h<26; h++){
      fscanf(input1,"%lf", &freqTwo[h]);
      printf("%lf\n", freqTwo[h]);

   }    
   fclose(input1);

   /*a = (freqOne[0]-freqTwo[0])*(freqOne[0]-freqTwo[0]);
     printf("%lf", a);*/
}

In my first attempt, i was able to print out all the values correctly, but I am not sure how to use them. I printed them out as strings, but when I try to print them out as %lf, it gave me 0's for every value. In my second attempt, I did some googling and found that I should try the fscanf function, but this did not work for either and a large negative number was printed out for every value. I am pretty stuck right now and out of ideas.

4
  • You need to use atof() function in C. Have look here Commented Nov 10, 2014 at 19:04
  • 3
    1) fscanf(input1,"%lf", &freqOne[i]); --> fscanf(input1,"%*s %lf", &freqOne[i]); to ignore the A, B, C, etc. 2) Check result of fscanf() Commented Nov 10, 2014 at 19:07
  • @hagubear rather strtod(). Commented Nov 10, 2014 at 19:08
  • hello everyone, thanks for the replies. @chux, your suggestion worked perfectly. I tested the values by printing them out and I did some calculations just to make sure everything works. Thank you for the help!! Commented Nov 10, 2014 at 19:31

1 Answer 1

0

So OP can close this post:

To ignore the A,B,C, etc., use assignment suppression '*'

// fscanf(input1,"%lf", &freqOne[i]);
fscanf(input1,"%*s %lf", &freqOne[i]);

Always a good idea to check I/O function results:

if (fscanf(input1,"%*s %lf", &freqOne[i]) != 1) Handle_Unexpected_Input();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.