Thread: Changing file pointer position

  1. #1
    Registered User
    Join Date
    Aug 2021
    Posts
    3

    Changing file pointer position

    Dear Friends

    I need your kind help urgently.
    I'm writing a piece of program under Ubuntu attempting to open an existing file to update (read/write).I need
    to move file pointer to any position in the file based on some calculations and alter the content. But the problem is that when I open the file using (fopen and "a"-mode) it writes always at the end of file and I cannot move backward(fseek has no effect). If I open it in "w"-mode it would be truncated (actually destroyed). The action (forcing the file pointer to set at any location of file) is something I used to do under DOS perfectly. Could you please suggest any solution for the issue?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you considered using the "+r" mode?

    Some documentation for the open modes of fopen.

  3. #3
    Registered User
    Join Date
    Aug 2021
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    Have you considered using the "+r" mode?

    Some documentation for the open modes of fopen.
    "r" combinations stand for "Read Only", don't they?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by Sanfter View Post
    "r" combinations stand for "Read Only", don't they?
    Please study the manpage for fopen() thoroughly, as jimblumberg gave you above! That will give you all the information you need!

  5. #5
    Registered User
    Join Date
    Aug 2021
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    Have you considered using the "+r" mode?

    Some documentation for the open modes of fopen.
    Dear jimblumberg

    Thank you so much for your advice. You solved my problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ - Get current position of file pointer.
    By mad-hatter in forum C++ Programming
    Replies: 9
    Last Post: 02-16-2017, 04:23 PM
  2. setting a file position pointer
    By JayCee++ in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2011, 06:40 AM
  3. Savign the position of a pointer in an input file
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 07-08-2005, 03:39 PM
  4. Replies: 4
    Last Post: 04-21-2005, 10:59 AM
  5. Position FILE pointer to a specified line of Text file
    By jessica_xie in forum C Programming
    Replies: 2
    Last Post: 02-04-2005, 03:52 PM

Tags for this Thread