In: Computer Science
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered.
Your program should check to make sure that the file was opened successfully, and terminate if it was not.
c code
#include <stdio.h>
main() {
FILE *fp;
fp = fopen("numbers.txt", "w");
if(fp==NULL)
{
exit(1);
}
float inputnum=-1;
char numtostring[50];
while(inputnum!=0)
{
scanf("%f",&inputnum);
gcvt(inputnum, 6, numtostring);
fprintf(fp, numtostring);
fprintf(fp,"\n");
}
fclose(fp);
}
Sample console output
55
6.7
056.2
0
Sample numbers.txt output
55
6.7
56.2
0