I am new in C. My code is as follows,which writes and then reads integer into/from a file. But its not running.Can i get help in finding out the problem in it. Also i need to do the read and write operation with floating point no.s which i am unable to do using putw() and getw(). Is there any other function for this?
Code:
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp,*f2;
int i,n,x[100],num;
int add,mul,y[10];
int h[10]={5,2,3,6,1};
clrscr();
printf("contents of DATA file");
fp=fopen("DATA.c","w");
/*writing integers as an array into "data" file*/
for(i=0;i<100;i++)
{
n=i;
x[n]=2*i;
putw(x[n],fp);
}
/*printing the written data*/
while ((num=getw(fp))!=EOF)
printf("%d",num);
fclose(fp
);
fp=fopen("DATA.c","r");
f2=fopen("result.c","w");
/*using the file "data" for further processing*/
while((num==getw(fp))!=EOF)
{
/*logic for convolution*/
for(n=0;n<=104;n++)
{
add=0;
for(i=0;i<=n;i++)
{
mul=num*h[n-i];
add=mul+add;
}
y[n]=add;
putw (y[n],f2);
}
}
fclose(fp);
fclose(f2);
/*printing the contents of file "result"*/
f2=fopen("result.c","r");
while((num=getw(f2))!=EOF)
printf("%d",num);
fclose(f2);
}
Comment