I'm trying to write a function which get numbers from the user and, put them in file and then reads them and find the minimun. this is the code I wrote but It doesn't work at all. can someone please help me understand what I'm doing wrong? I'm new to C.
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
int min_call(int, ...);
int main()
{
int min;
min = min_call(90,78,5,20,-1);
printf("\n the minimum number is: %d ", min);
min = min_call(70,40,2,-1);
printf("\n the minimum number is: %d ", min);
min = min_call(40,30,-1);
printf("\n the minimum number is: %d ", min);
return 0;
}
int min_call(int first, ...)
{
int min;
int currentNum;
int i;
va_list args;
va_start(args,first);
FILE *fd;
if(!(fd=fopen("min_call_file.txt","a")))
{
fprintf(stderr, "cannot open file \n");
exit (0);
}
for(i = first; i>=0; i=va_arg(args, int))
{
fprintf(fd, "%d", i);
}
va_end(args);
fseek(fd,0,SEEK_SET);
min = fgetc(fd);
do
{
currentNum = fgetc(fd);
if(currentNum < min)
min = currentNum;
}while(!feof(fd));
fclose(fd);
return min;
}