Can't update text file
-
:confused:This should be a no brainer. I am using Visual C++ 6.0. Because I want to use fseek() I declare a file of FILE* filename and then use filename = fopen(path, "r+"). I can read from the file but when I try to write (using fprintf() and fwrite()) to the file nothing changes in the file I am reading from and trying to write to. I have also tried "rt+". I receive no errors. Is there something basic I'm not aware of? Buck
-
:confused:This should be a no brainer. I am using Visual C++ 6.0. Because I want to use fseek() I declare a file of FILE* filename and then use filename = fopen(path, "r+"). I can read from the file but when I try to write (using fprintf() and fwrite()) to the file nothing changes in the file I am reading from and trying to write to. I have also tried "rt+". I receive no errors. Is there something basic I'm not aware of? Buck
See if this helps! When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.
-
:confused:This should be a no brainer. I am using Visual C++ 6.0. Because I want to use fseek() I declare a file of FILE* filename and then use filename = fopen(path, "r+"). I can read from the file but when I try to write (using fprintf() and fwrite()) to the file nothing changes in the file I am reading from and trying to write to. I have also tried "rt+". I receive no errors. Is there something basic I'm not aware of? Buck
Hi, According to the MSDN library at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/\_crt\_fopen.2c\_.\_wfopen.asp they say "When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired." So it looks like you will have to reset the open file pointer to the appropriate position before switching between reading and writing operations. If possible, it may be more convenient for you to read and write using separate sessions. Hope this helps. Paul