Reading a file.dat, applying calculations

This is a discussion on Reading a file.dat, applying calculations within the C Programming forums, part of the General Programming Boards category; Hello, this is my first post on these boards, I have seen alot of helpfull responses, this website tends to ...

  1. #1
    Registered User airgunner's Avatar
    Join Date
    Jan 2007
    Posts
    7

    Reading a file.dat, applying calculations

    Hello, this is my first post on these boards, I have seen alot of helpfull responses, this website tends to be my first call when i have problems.

    I have a file created by another program called sin.dat, the file is an array of 60 numbers that have been saved:

    0.00
    3.09
    5.88
    8.09
    9.51
    10.00
    9.51
    8.09
    5.88
    3.09
    -0.00
    -3.09
    -5.88
    -8.09
    -9.51
    -10.00
    -9.51
    -8.09
    -5.88
    -3.09
    0.00
    3.09
    5.88
    8.09
    9.51
    10.00
    9.51
    8.09
    5.88
    3.09
    -0.00
    -3.09
    -5.88
    -8.09
    -9.51
    -10.00
    -9.51
    -8.09
    -5.88
    -3.09
    0.00
    3.09
    5.88
    8.09
    9.51
    10.00
    9.51
    8.09
    5.88
    3.09
    -0.00
    -3.09
    -5.88
    -8.09
    -9.51
    -10.00
    -9.51
    -8.09
    -5.88
    -3.09

    Plotting this in excell gives a sin wave of amplitude 10.

    Now my problem doesn't lie here, it is the program I wrote to load this file and use it.

    My code:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    #define pi 3.1416
    
    float array[60], ary[2];
    int index=0, i;
    char infilename[20];
    float vpk, a1, a2;
    float h=1e-3, w;
    
    
    int main()
    {
     FILE *infile;
     printf("Please enter a filename\n");
     gets(infilename);
     if((infile=fopen(infilename,"r"))==NULL); //I believe the problem lies here//
     {
      printf("\nThere has been a problem locating or reading the specified file,\nPlease ensure you have typed the filename correctly.\n"); 
     }
     
     while(!feof(infile)||index<60)
     {
      fscanf(infile,"%f\n",&array[index]);
      printf("%f\n",array[index]);
      index++; 
     }
     
     
     printf("Now calculating the peak voltage...\n");
     
     for(i=0;i<59;i++)
     {
      ary[i+2]=ary[i+1];
      ary[i]=ary[i+1];
      ary[i]=array[index];
     
      a1=(ary[i+1]*ary[i+1]);
      a2=((ary[i+2]-ary[i])/2*h);
      w=(2*pi*50);
     
      vpk=a1+((a2*a2)/(w*w));
     }
      printf("%f",vpk);
     
     fclose(infile);
    }
    When I compile, build then execute, I allways get "There has been a problem locating or reading the specified file, Please ensure you have typed the filename correctly" and then it displays all 60 numbers from the saved array.

    I can't seem to get my head round why it shows this messege. As there is no problem with opening the array, it displays the contents of it on screen. Feel free to copy and paste this.

    I know there are a few problems with the working after "printf("Now calculating the peak voltage...\n");", these problems I plan to resolve after. In this part I am trying to find the peak amplitude of the sine wave, using the equation: Vpk^2=(Vt+1)^2+[(Vt+2-Vt)/2*h]^2/w^2.

    I may not have explained this very well, but any advice would be appreciated.

    Many Thanks.
    Last edited by airgunner; 01-15-2007 at 12:02 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > gets(infilename);
    Nah, this is your first problem - see the FAQ

    > //I believe the problem lies here//
    Your belief is correct - now study the line in detail to see which character should not be there.

    > while(!feof(infile)||index<60)
    1. You should not use feof() in control loops - see the FAQ
    2. it should be &&, not ||
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User airgunner's Avatar
    Join Date
    Jan 2007
    Posts
    7
    Thaks for the swift reply,

    Ah, so fgets is secure and limits the reading.
    Would I have to include it and define it like this?

    char *fgets(char *s, int size, FILE *stream);

    then later... fgets(array,60,infilename)
    This has thrown me slightly...

    HAHA! I guess it's the ";" thrown on the end! ~ Resolved

    I see how feof can be a problem now, it would be better to use fgets() and check the code returned.
    Although I am a little unsure on how to use fgets(), (this ones new to me).

  4. #4
    Registered User airgunner's Avatar
    Join Date
    Jan 2007
    Posts
    7
    Can you take a look through this?

    Code:
    printf("Now calculating the peak voltage...\n");
     
     for(i=0;i<59;i++)
     {
      ary[i+2]=ary[i+1];
      ary[i]=ary[i+1];
      ary[i]=array[index];
     
      a1=(ary[i+1]*ary[i+1]);
      a2=((ary[i+2]-ary[i])/2*h);
      w=(2*pi*50);
     
      vpk=a1+((a2*a2)/(w*w));
     }
      printf("%f",vpk);
    What I have attemted to do is take the loaded array, create another array of 3 slots and take the first 3 values from the loaded array and put them into the array of 3. Ughh, after reading that back to myself I'd be suprised if you can make sense of it! I'm trying to do this,
    Code:
      ary[i+2]=ary[i+1];
      ary[i]=ary[i+1];
      ary[i]=array[index];
    But it may not be right as when I execute the program, it outputs 0. It also outputs 0 for a1 and a2, if this helps.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    don't you see that ary[i] is initialized twice, and ary[i+1] never?
    also, where is the local array of 3?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User airgunner's Avatar
    Join Date
    Jan 2007
    Posts
    7
    so i should change it to:
    ary[i+2]=ary[i+1];
    ary[i+1]=ary[i];
    ary[i]=array[index];
    This isn't what you mean is it?

    The array of 3 is: ary[2]
    float array[60], ary[2];
    Vt should go into 0, Vt+1 into 1 and Vt+2 into 2...

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if ary is declared in that way the possible indexes are from 0 to 1
    you cannot access any other member of the array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User airgunner's Avatar
    Join Date
    Jan 2007
    Posts
    7
    Quote Originally Posted by vart
    if ary is declared in that way the possible indexes are from 0 to 1
    you cannot access any other member of the array
    So ary[2] should infact be ary[3] for starters?
    Can you follow that snipit of code, does it make sense to you?

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    for(i=0;i<58;i++)
    {
       float *ary = array+i;
     
      a1=(ary[1]*ary[1]);
      a2=((ary[2]-ary[0])/2*h);
      w=(2*pi*50);
     
      vpk=a1+((a2*a2)/(w*w));
    }
    And of course w*w should be calculated outside the loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User airgunner's Avatar
    Join Date
    Jan 2007
    Posts
    7
    Thanks, That makes more sense.
    I have a few adjustments and corrections to the program to make.
    I will update with the outcome.

  11. #11
    Registered User airgunner's Avatar
    Join Date
    Jan 2007
    Posts
    7

    Thumbs up

    3 more hours of typing and de-bugging and i'm done
    Thanks for all your advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 11:36 PM
  2. reading in file data
    By sickofc in forum C Programming
    Replies: 2
    Last Post: 03-01-2006, 05:16 PM
  3. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  4. file reading
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2002, 11:55 PM
  5. Reading from a file
    By Wiz_Nil in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21