注册 登录
编程论坛 C++教室

[求助]使用fwrite遇到的问题

幽园香客 发布于 2007-10-18 15:57, 689 次点击
如题,第一次写了一批数据到一个文件中,然后关闭该文件;再打开该文件,接着上次写的末尾(或者在另外的某个位置),再写数据进去,该如何操作.先谢谢各位咯!
3 回复
#2
cince2007-10-18 16:34
修改它的默认的就行了.

它的默认的是截取为0的..也就是会覆盖原来的.
#3
HJin2007-10-19 00:29

/*---------------------------------------------------------------------------
File name: bccn-使用fwrite遇到的问题.c
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 10/18/2007 09:28:24
Environment: WinXPSP2 En Pro + VS2005 v8.0.50727.762


Modification history:
===========================================================================


Problem statement:
---------------------------------------------------------------------------
https://bbs.bc-cn.net/viewthread.php?tid=178760

[求助]使用fwrite遇到的问题

如题,第一次写了一批数据到一个文件中,然后关闭该文件;再打开该文件,接着上次写的末尾(或者在另外的某个位置),再写数据进去,该如何操作.先谢谢各位咯!
*/

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

int main()
{
FILE* fp;
int i;

char filename[] = "a.txt";
fp = fopen(filename, "w");
fwrite("abcd", 4, 1, fp); //abcd
fclose(fp);

fp=fopen(filename, "a");
fwrite("efg", 3, 1, fp); //abcdefg
fclose(fp);

fp=fopen(filename, "r+");
fseek(fp, 1, SEEK_SET);
fwrite("hi", 2, 1, fp); //ahidefg
fclose(fp);

return 0;
}

#4
幽园香客2007-10-19 10:23
哦,我知道该怎么去做了,谢谢斑竹!
1