0

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,
stella
  • 23
  • 6
  • 1
    Paths are relative to your current working directory, not to the location of the source file. – zvone Aug 28 '23 at 08:05
  • 1
    It's probably that the file does not exist or a permission problem – klutt Aug 28 '23 at 08:05
  • 1
    Now is the time to learn about the concept of [*working directory*](https://en.wikipedia.org/wiki/Working_directory). When you use a relative path, like `input.txt`, it's relative to the process working directory. If the working directory is not what you expect, the file will not be found. If you use a full and absolute path it should work. – Some programmer dude Aug 28 '23 at 08:06
  • 1
    Did you mean to use the path like this "/home/debian/progdir/input.txt" ? – stella Aug 28 '23 at 08:16
  • 1
    OT: Change `#include "stdio.h"` to `#include `... This is another place where "path" is important... For the moment, it's a good rule to only use double quotes when naming your own _header_ files. Use angle brackets when naming headers that are part of the C distribution... – Fe2O3 Aug 28 '23 at 08:19
  • @stella Yes that's correct. – Some programmer dude Aug 28 '23 at 08:20
  • 3
    A side note: try to put `perror(srcFilePath)` instead of your own `printf("** Failed ...")`. Then you can narrow down the cause of error such as `No such file or directory` or `Permission denied` (although the cause is mostly obvious in this case). – tshiono Aug 28 '23 at 08:26
  • The same error!! – stella Aug 28 '23 at 08:26
  • Is the new path the actual, real and full absolute path to the file? If you do as @tshiono says and use `perror(srcFilePath)` to print the error, what does it tell you? Please [edit] your question with the message reported by `perror(srcFilePath)`. – Some programmer dude Aug 28 '23 at 08:31
  • @tshiono, When I used perror(srcFilePath) ; I got on /home/debian/progdir/input.txt: No such file or directory. The text file and the prog.c in the same progdir directory – stella Aug 28 '23 at 08:31
  • In a terminal window, using a shell, run the command `ls -l /home/debian/progdir/input.txt`. What does it tell you? If you run `ls -l /home/debian/progdir/` what files are listed? – Some programmer dude Aug 28 '23 at 08:32
  • Take a look at this https://stackoverflow.com/questions/298510/how-to-get-the-current-directory-in-a-c-program Use that code to find out your cwd – klutt Aug 28 '23 at 08:32
  • And please **[edit]** your question to include new important details like the error and the file listing. – Some programmer dude Aug 28 '23 at 08:34
  • @Someprogrammerdude this is the output: -rw-r--r-- 1 debian debian 429 Aug 28 04:28 fi.c -rw-r--r-- 1 debian debian 240 Aug 27 16:11 input -rwxr-xr-x 1 debian debian 16848 Aug 28 04:36 p – stella Aug 28 '23 at 08:38
  • @stella Any chance that "input" and "input.txt" are thought to be the same filename? – Fe2O3 Aug 28 '23 at 08:41
  • @stella It would be easier to read if you did what I asked you, and added it to your question. But what I can see is that there is a file named `input`. But no one named `input.txt`. – Some programmer dude Aug 28 '23 at 08:42
  • The error message `/home/debian/progdir/input.txt: No such file or directory` looks strange to me. As the pointer `inputfile` and `srcFilePath` just point to the string `"input.txt"` assigned in main(), the program and perror() shouldn't know its *absolute path*. – tshiono Aug 28 '23 at 09:45
  • Thank you for all the help. could you see the update please? Or I should open new question? – stella Aug 28 '23 at 11:35
  • 2
    @stella If you have other, new and unrelated problems, then please post a new question. One question per question. :) – Some programmer dude Aug 28 '23 at 11:35

1 Answers1

0

Note: you need to modify below code in main:

const char inputfile[]="/home/hi/my-document/dcn/input.txt";

My .c file is:

#include <stdio.h>

#define lineNum 8

void func(const char *srcFilePath)
{
    FILE *pFile = fopen(srcFilePath, "r");
    float daBuf[10] = {0};
    int c = 0;
    if (pFile == NULL)
    {
        //printf("** Failed to open input file. 1\n");
        perror(srcFilePath) ;
    }
    else
    {
        while(!feof(pFile) && c < lineNum){
            fscanf(pFile,"%f",&daBuf[c]);
            printf("daBuf[%d] = %f\n", c, daBuf[c]); 
            c++;
        }
    }

    printf("**************************************************\n");

    for(int i = 0; i < lineNum; i++) {
        printf("daBuf[%d]  = %f\n", i, daBuf[i]);
    }

}    

void main()
{
    const char inputfile[]="/home/hi/my-document/dcn/input.txt";
    func(inputfile);
}

And input.txt is:

1.589211
1.557358
1.525398
1.493311
1.483532
1.483766
1.654107
1.621582

Output is:

daBuf[0] = 1.589211 
daBuf[1] = 1.557358 
daBuf[2] = 1.525398 
daBuf[3] = 1.493311 
daBuf[4] = 1.483532 
daBuf[5] = 1.483766 
daBuf[6] = 1.654107 
daBuf[7] = 1.621582 
**************************************************
daBuf[0]  = 1.589211 
daBuf[1]  = 1.557358 
daBuf[2]  = 1.525398 
daBuf[3]  = 1.493311 
daBuf[4]  = 1.483532 
daBuf[5]  = 1.483766 
daBuf[6]  = 1.654107 
daBuf[7]  = 1.621582
Tom
  • 417
  • 3
  • 10
  • Thank you for all the help. could you see the update please? Or I should open new question? – stella Aug 28 '23 at 11:36
  • @stella Please give a sample, for example what have input.txt? – Tom Aug 28 '23 at 11:37
  • I posted a simple of the input data. – stella Aug 28 '23 at 11:46
  • I know that at least in C++ you can use forward slashes in the path on windows. Is this different in C? – infinitezero Aug 28 '23 at 11:46
  • @stella it may be relative to escape sequences. \\ represents backslash in c program. – Tom Aug 28 '23 at 11:52
  • @stella Please see my updated answer. – Tom Aug 28 '23 at 12:23
  • Thanks Tom, what is the problem in fread(daBuf,sizeof(float), 1, pFile); ? why I can not read with it? – stella Aug 28 '23 at 14:31
  • @stella Read [**Why is “while( !feof(file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong). The addition of `if(feof(pFile))` in this answer doesn't do anything to fix that - in fact, that addition completely ignores the fact that it's repeating one of the tests in the `while()` statement above it - if that test were true, the loop would have ended. The added `if(feof(pFile))` test is useless. `feof()` won't be true until an attempt is made to read **past** the end of the file - and that read will then fail. – Andrew Henle Aug 28 '23 at 16:38
  • @AndrewHenle Thank you for point out my question, I changed my answer. – Tom Aug 28 '23 at 21:47
  • @stella sizeof(float) is 4 in 32-bit system computer. char* str = "1.589211"; strlen(str) = 8. And daBuf in `fread(daBuf,sizeof(float), 1, pFile);` is not right, because daBuf is equal to &daBuf[0], it don't changed in `while(!feof(pFile))`, it should be such as &daBuf[0], &daBuf[1], &daBuf[2] ...... – Tom Aug 28 '23 at 22:34