1

I've function that opens up a file and than setting it pos from nvs at the program start-up. Everything happens after short time and function call end up with error that says [Line : 670] fseek ERROR : Function not implemented. After short period of time another call works.

I wonder what may cause the problem becouse before I use this specific example with fseek I do multiple fopen/fclose on different files.

        FILE *file;
        file = fopen("/spiffs/data/log.log", "rb+");
        if(file == NULL) return;
        if(fseek(file, 30, SEEK_SET) != 0) {
            fprintf(stderr, "[Line : %d] fseek ERROR : ", __LINE__);
            perror("");
        }

File itself is full of data and it length is 10kB

@edit after what @Andrew Henle said in the comment I have looked into what fseek can actualy return. I find out under this link that fseek has no ENOSYS as return. Which means his point is right but Iam making lots of fopen/fclose/fread before this function call. What conditions has to occur for this error to be returned by f* functions?

Coffeeye
  • 31
  • 6
  • 2
    I would at least check that `file` is valid and that `fopen()` didn't return `NULL`. – sj95126 Oct 24 '22 at 15:05
  • 2
    The value of `errno` you're getting from `perror()` is not necessarily correct. Your intervening call to `fprintf()` [can change `errno`](http://port70.net/~nsz/c/c11/n1570.html#7.5p3): "The value of `errno` may be set to nonzero by a library function call whether or not there is an error, provided the use of `errno` is not documented in the description of the function in this International Standard." – Andrew Henle Oct 24 '22 at 15:38
  • @sj95126 Sorry I provided function without this checking, what I do is just printf a string for the console if file is null and return. I shall update this post in notime. – Coffeeye Oct 24 '22 at 15:46
  • @AndrewHenle so what you are telling me is that may be not caused by fseek call? but surely fseek isnt 0 there. Which means it failed at some point. Do you know what may it be? – Coffeeye Oct 24 '22 at 15:48
  • 1
    @Coffeeye Please change the order of the `perror()` call and the `fprintf(stderr,...` call as other has mentioned so you are certain you are seeing the correct error message. Also tell us a bit about what kind of platform you're running your code on. – nos Oct 24 '22 at 16:12
  • @nos its ESP32 with esp-idf v4.4 framework runnin on it. – Coffeeye Oct 24 '22 at 16:16
  • @AndrewHenle Your answer were correct, I moved perror above fprintf and I got error which I'am familiar with, I/O Error. My case is multiple thread trying to write at once I supouse the problems lays there. Thanks for your contributions! You might add it as an answear or shall I just delete this thread becouse its confusing. – Coffeeye Oct 25 '22 at 06:07

1 Answers1

0

Is it possible for you to post the file in question?

What i can understand is that the file is being opened and the fseek is doing its job

https://www.tutorialspoint.com/c_standard_library/c_function_fseek.htm

Try to see what value the fseek gets and if the file ends before 30.

You could use fgets(), but i'm pretty sure fseek is the best method to what your doing.

Filipe
  • 146
  • 10