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.