cpubbs论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

LabVIEW+单片机学习套件全套教程资料下载[免费]LabVIEW论坛精华列表贴USB0816数据采集卡《LabVIEW宝典》
LabWindows/CVI论坛精华贴NET0816以太网数据采集卡RC0210远程设备授权系统 关闭关停锁定打开设备 户外分布式数据采集
NET1624低速高精度以太网数据采集卡WIFI0824SD无线WIFI网络数据采集卡脱机运行 SD存储 小尺寸微型 串口采集远程采集 安卓 手持移动采集 纪录仪
查看: 1132|回复: 5

请问关于文件的一个问题

[复制链接]
发表于 2008-11-17 21:31:44 | 显示全部楼层 |阅读模式
比如我建了一个文件,程序每次要向文件里写内容.但是发现每次写都是将原来的内容覆盖.请教如何实现在文件内容的末尾继续添加.因为以前的内容还要保留.
发表于 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);
发表于 2008-11-18 00:10:35 | 显示全部楼层
设定指针的位置!

不知道兄弟用的是哪个函数来操作的?
 楼主| 发表于 2008-11-18 09:06:11 | 显示全部楼层
首先谢谢BBS的答复,
我用openfile()打开一个文件,然后用writeline()写,然后关闭文件.
请问怎么用指针设定位置?我如何知道每次执行文件写的地址?
我英语不大好上边那段没看大懂  -_- !

新手,问题可能有点可笑,但我确实不会!
发表于 2008-11-18 09:13:10 | 显示全部楼层
将int OpenFile (char *fileName, int read/writeMode, int action, int fileType);函数中的action参数设置为:VAL_APPEND即可;
设置为:VAL_TRUNCATE会将原来的内容清除!
 楼主| 发表于 2008-11-18 09:35:30 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|cpubbs论坛. ( 粤ICP备09171248号 )

GMT+8, 2025-4-20 12:49 , Processed in 1.304856 second(s), 7 queries , Gzip On, File On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表