fopen文件追加的问题

rotApple 2007-08-02 10:49:02
怎么样往文件的前面追加? 而不是往最后面追加?
...全文
1420 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
rotApple 2007-08-02
  • 打赏
  • 举报
回复
FILE* pf = fopen(path,_T("a+"));

这返回的是FILE指针啊. 不是HANDLE
fantasyemperor 2007-08-02
  • 打赏
  • 举报
回复
SetFilePointer 使用的是CreateFile产生的句柄
unicode 2007-08-02
  • 打赏
  • 举报
回复
先打开得到handle,然后再set不可以吗
rotApple 2007-08-02
  • 打赏
  • 举报
回复
unicode(衣不如新,人不如故) ( ) 信誉:100 2007-08-02 10:57:53 得分: 0


DWORD SetFilePointer(
HANDLE hFile,
LONG lDistanceToMove,
PLONG lpDistanceToMoveHigh,
DWORD dwMoveMethod
);

dwMoveMethod:
FILE_BEGIN Indicates that the starting point is zero or the beginning of the file.
FILE_CURRENT Indicates that the starting point is the current value of the file pointer.
FILE_END


===================================================


他的这个应该是可行的, 只是用fopen后怎么得到打开的文件的句柄??
xdljf 2007-08-02
  • 打赏
  • 举报
回复
文件在硬盘上是顺序存放的,在前面追加有两种方式:
1.不改变文件的内容,把文件记录信息向前移
2.把文件的内容向后移,插入追加的信息,文件记录信息不变

第一种方式OS很难实现的哦
rotApple 2007-08-02
  • 打赏
  • 举报
回复
DWORD SetFilePointer(
HANDLE hFile,


如果是fopen的话怎么取得hFile?
rotApple 2007-08-02
  • 打赏
  • 举报
回复
那如果该文本文件原内容爆大不就挂掉了, 这个方法简单,但不好,也不实用.
unicode 2007-08-02
  • 打赏
  • 举报
回复
DWORD SetFilePointer(
HANDLE hFile,
LONG lDistanceToMove,
PLONG lpDistanceToMoveHigh,
DWORD dwMoveMethod
);

dwMoveMethod:
FILE_BEGIN Indicates that the starting point is zero or the beginning of the file.
FILE_CURRENT Indicates that the starting point is the current value of the file pointer.
FILE_END
id_oyou 2007-08-02
  • 打赏
  • 举报
回复
可以先另存一下文件,再重写文件,只要最后加上刚才另存的原来文件应该就可以了。
rotApple 2007-08-02
  • 打赏
  • 举报
回复
已经解决!

#include <stdlib.h>
#include <stdio.h>

void showpos(FILE *stream);

int main(void)
{
FILE *stream;
fpos_t filepos;

/* open a file for update */
stream = fopen("DUMMY.FIL", "w+");

/* save the file pointer position */
fgetpos(stream, &filepos);

/* write some data to the file */
fprintf(stream, "This is a test");

/* show the current file position */
showpos(stream);

/* set a new file position, display it */
if (fsetpos(stream, &filepos) == 0)
showpos(stream);
else
{
fprintf(stderr, "Error setting file \
pointer.\n");
exit(1);
}

/* close the file */
fclose(stream);
return 0;
}

void showpos(FILE *stream)
{
fpos_t pos;

/* display the current file pointer
position of a stream */
fgetpos(stream, &pos);
printf("File position: %ld\n", pos);
}



谢谢各位.

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧