hbf 发表于 2008-11-17 21:31:44

请问关于文件的一个问题

比如我建了一个文件,程序每次要向文件里写内容.但是发现每次写都是将原来的内容覆盖.请教如何实现在文件内容的末尾继续添加.因为以前的内容还要保留.

cpubbs 发表于 2008-11-18 00:10:14

Moves the file pointer for the file specified by fileHandle to a location that is offset bytes from origin. Returns the offset of the new file pointer position from the beginning of the file.

You can use SetFilePtr to obtain the file size by setting offset to 0 and origin to 2. In this case, the return value indicates the file size, and the file pointer points to the end of the file.

You can position the file pointer beyond the end of the file. Intermediate bytes, bytes between the old end of file and the new end of file, contain values that might vary. An attempt to position the file pointer before the beginning of the file causes the function to return an error.

If the file is a device that does not support random access, such as the Standard Input, the function returns a value that might vary.

Example
/* Open or create the file c:\TEST.DAT, move 10 bytes into the file, and write a string to the file. */

/* Note: In C, use \\ in pathname instead of \. */

int handle, result;
long position;

handle = OpenFile("c:\\TEST.DAT", 0, 2, 1);
if (handle == –1){
   FmtOut("error opening file");
   exit(1);
}
position = SetFilePtr(handle, 10L, 0);
if (position == 10){
   result = WriteFile(handle, "Hello, World!", 13);
   if (result == -1)
      FmtOut("error writing to file");
}
else
   FmtOut("error positioning file pointer");
CloseFile(handle);



Prototype

long SetFilePtr (int File_Handle, long Offset, int Origin);

cpubbs 发表于 2008-11-18 00:10:35

设定指针的位置!

不知道兄弟用的是哪个函数来操作的?

hbf 发表于 2008-11-18 09:06:11

首先谢谢BBS的答复,
我用openfile()打开一个文件,然后用writeline()写,然后关闭文件.
请问怎么用指针设定位置?我如何知道每次执行文件写的地址?
我英语不大好上边那段没看大懂-_- !
allqq-mogudiandian-35all
新手,问题可能有点可笑,但我确实不会!

netcreature 发表于 2008-11-18 09:13:10

将int OpenFile (char *fileName, int read/writeMode, int action, int fileType);函数中的action参数设置为:VAL_APPEND即可;
设置为:VAL_TRUNCATE会将原来的内容清除!

hbf 发表于 2008-11-18 09:35:30

谢谢!我再试一下
页: [1]
查看完整版本: 请问关于文件的一个问题