|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 空蓝 于 2024-4-17 16:35 编辑
#include <fstream>
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main()
{
fstream fp("test.txt", ios::in | ios::out);
if (!fp)
{
cerr << "打开文件失败!" << endl;
return 0;
}
fp.seekp(ios::end); // 使得文件指针指向文件头 ios::end 则是文件尾。
fp << "IloveFishc.com!";
static char str[100];
fp.seekg(ios::beg); // 使得文件指针指向文件头 ios::end 则是文件尾。
fp >> str;
cout << str << endl;
fp.close();
return 0;
}
使用 fp.seekp(ios::end); 想把写入指针指向文件末尾,并写入 IloveFishc.com 实际上是在第三个字符处开始写入的.
原文内容:123456789
写入后内容:12IloveFishc.com!
本帖最后由 jackz007 于 2024-4-17 16:56 编辑
- #include <fstream>
- #include <iostream>
- #include <sys/stat.h>
- using namespace std;
- int main()
- {
- fstream fp("test.txt", ios::in | ios::out);
- if (!fp)
- {
- cerr << "打开文件失败!" << endl ;
- return 0;
- }
- fp.seekp(0L , ios::end) ; // 此处有修改
- fp << "IloveFishc.com!" ;
- static char str[100];
- fp.seekg(0L , ios::beg) ; // 此处有修改
- fp >> str;
- cout << str << endl ;
- fp.close() ;
- return 0 ;
- }
复制代码
别忘记每次运行都得重新初始化文件 "test.txt" 的内容。
|
|