Write a program to compute the following equation for values of time.
y = 2x+3
The values of time are stored in the file program.dat and the solution should be displayed on the screen and stored in a file called program.out.
Answer
#include stdio.h
#include stdlib.h
void main()
{
char prompt;
Date: 26th August 2012
Version 1.0
Function : ASCII file handling
Modifications: none*/
/* Define I/O streams */
FILE *fptr,*fptw;
float x,y;
fptr = fopen("program.dat","r");
if (fptr == NULL)
{
/* Check for error */
printf("Cannot find the file program.dat\n\r");
exit(1);
}
fptw = fopen("program.out","w");
if (fptw == NULL)
{
/* Check for error */
printf("Cannot create a new file called program.out");
fclose(fptr);
exit(1);
}
while (fscanf(fptr,"%f",&x) != EOF)
{
/* This repeats the read till fscanf returns back an End of file marker i.e No
data left */
y = 2*x+3;
printf("X = %f Y=%f\n\r",x,y);
fprintf(fptw,"X = %f Y=%f\n\r",x,y);
}
printf("I have finished the computation\n\r");
fclose(fptr);
fclose(fptw);
printf("Press and key to exit \n\r");
scanf("\n%c",&prompt);
}
Original data can be entered using the application 'NOTEPAD', or 'Edit' likewise the output can be read by Notepad or any word processor set to text mode.