In this code I am trying to pass a file to function and then read it inside the function. I got on this error:
** Failed to open input file. 1
The input.txt file inside the folder of my code.
How can I fix it please?
#include "stdio.h"
void func(const char *srcFilePath)
{
FILE *pFile = fopen(srcFilePath, "r");
float num;
if (pFile == NULL)
{
printf("** Failed to open input file. 1\n");
}
else
{
fscanf(pFile,"%f",num);
fclose(pFile);
}
printf("num = %f",num);
}
void main()
{
const char inputfile[]="input.txt";
func(inputfile);
}
UPDATE:
After run the proposed solution, now I have two problem
1- If I use fscanf(pFile,"%f",&num);
for reading the program inter in loop.
2- If I use fread(daBuf,sizeof(float), 1, pFile);
there is nothing read in daBuf[] the output is just 0.000 .
#include <stdio.h>
void func(const char *srcFilePath)
{
FILE *pFile = fopen(srcFilePath, "r");
float num;
float daBuf[24];
if (pFile == NULL)
{
//printf("** Failed to open input file. 1\n");
perror(srcFilePath) ;
}
else{
while(!feof(pFile)){
//fscanf(pFile,"%f",&num);
//printf("num = %f \n",num);
fread(daBuf,sizeof(float), 1, pFile);
}
}
for(int i=0;i<5;i++) {
printf(" daBuf = %f \n",daBuf[i]);
}
}
void main(){
const char inputfile[]="/home/debian/progdir/input";
func(inputfile);
}
This is a simple of input file:
1.589211,
1.557358,
1.525398,
1.493311,
1.483532,
1.483766,
1.654107,
1.621582,