fseek() Function | C Programming Tutorial

Поділитися
Вставка
  • Опубліковано 11 лис 2024

КОМЕНТАРІ • 24

  • @nathanaelmoh5848
    @nathanaelmoh5848 3 місяці тому

    Thanks for the great explanation. The book I'm reading says that fseek() and ftell() are not guaranteed to work with text files as with binary files so I had to come see if your channel had a video. Excellent as always. Hoping for an ftell() video soon!

  • @carlitobrigante9740
    @carlitobrigante9740 2 роки тому +12

    best explanation ever sir...

  • @vinaldotambunan8117
    @vinaldotambunan8117 Рік тому

    thank you sir really easy to understand love from suramadu

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      You're welcome Vinaldo! :-) I'm really glad you found it easy to understand!

  • @mongraal2272
    @mongraal2272 2 роки тому +5

    but why it prints only 2 asterisks intead of 4 at the end??
    Why 'j' isnt repleased by '*'?

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +11

      We only print two asterisk chars with:
      fprintf(file, "**");
      So that is all we can expect to appear.
      The end of the file looks like this:
      ...
      g
      h
      i
      j


      EOF

    • @mongraal2272
      @mongraal2272 2 роки тому +4

      @@PortfolioCourses thanks!!

  • @gamingcomputers7485
    @gamingcomputers7485 11 місяців тому

    its probably a year or 2 since this video but can we specify prefix numbers if its either text or binary like 0x15 or whatever like that? also i see that fprintf is writing to a file huh? maybe it different for binary one like fwrite?

  • @germankoga8640
    @germankoga8640 2 роки тому +1

    Maybe is more uncomfortable but, since the file handle is a pointer, could we do this of moving the position with pointer aritmethic too? (tho I would never)

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      I've never tried that, I don't think that's something that can be done unless a compiler decided to support it somehow: stackoverflow.com/questions/57897105/incrementing-and-decrementing-a-file-pointer.

  • @oaly3561
    @oaly3561 Рік тому

    thank you for that explanation but i have a question, are there any differences between rewind() and fseek() for when you want to place cursor at the beginning?

    • @nathanaelmoh5848
      @nathanaelmoh5848 3 місяці тому

      From the Ubuntu man page for fseek: The rewind() function sets the file position indicator for the stream pointed to by stream to the beginning of the file. It is equivalent to:
      (void) fseek(stream, 0L, SEEK_SET)
      except that the error indicator for the stream is also cleared (see clearerr(3)).

  • @bbatroll
    @bbatroll 11 місяців тому

    the goat 🐐🔥

  • @MonalPrakash
    @MonalPrakash 3 місяці тому

    Fseek returns 0 when operation is successful

  • @darrenli2526
    @darrenli2526 Рік тому

    thank you so much !

  • @alexiszhou2103
    @alexiszhou2103 Рік тому

    Hi sir, can I ask how to use ftell? As it might relate to fseek :)

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      Great question Alexis. I don't have a video on ftell() yet, so maybe an online resource like this can help you in the meantime: www.tutorialspoint.com/c_standard_library/c_function_ftell.htm. :-)

  • @JameBold
    @JameBold Рік тому

    I've often read that fseek() returns zero if it succeeds. Now I'm bit confused what is right. But thanks for the video.

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      Online sources indicate that it returns zero if it succeeds too, I'll have to look into this.

    • @nathanaelmoh5848
      @nathanaelmoh5848 3 місяці тому

      @@PortfolioCourses from the Ubuntu man page for fseek: The rewind() function returns no value. Upon successful completion, fgetpos(), fseek(), fsetpos() return 0, and ftell() returns the current offset. Otherwise, -1 is returned and errno is set to indicate the error.

  • @wc3815
    @wc3815 2 роки тому

    Could i ask, how do you count for if you fseek(file, -50, SEEK_END) but the file does not have any byte from 50 from the end?
    I found that c=fgetc(file) != EOF doesnt catch the error here 😭

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому

      It should set ferror if this occurs: en.cppreference.com/w/c/io/fseek. And then you could use ferror(file_pointer) to detect the error, e.g.: en.cppreference.com/w/c/io/ferror. :-)