简体   繁体   中英

C read/write and clear FILE

this might sound like a dumb question, but I understand fopen(fileLoc, r+); opens an existing file and allows both read and write operations to this file. However, how would I get fopen to clear the file aswell, to allow w like write access to clear file, but also allows read access, I have tried r+w but it doesnt seem to work properly.

So I need essentially both fopen(fileLoc, r); and fopen(fileLoc, w); access to a file.

fopen(fileLoc, w+) should do it. See http://en.wikibooks.org/wiki/C_Programming/C_Reference/stdio.h/fopen

Update: Also, you might need to call positioning function (like fseek() or fgetpos() ) in between the output and input operations, in accordance with the ANSI C standard: http://man7.org/linux/man-pages/man3/fopen.3.html

您可以尝试使用fopen(fileLoc,w +)进行读写。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM