c++如何让将txt文档信息放入数组使用再将存入数组的信息存入文档

dreaming ing 2018-09-19 08:26:51
c++如何让将txt文档信息放入数组使用再将存入数组的信息存入文档,求大神解答。
...全文
260 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dreaming ing 2018-09-20
  • 打赏
  • 举报
回复
引用 4 楼 沐雨青城 的回复:
用文件流就可以了
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream file;
string filename = "test.txt";
string content, tmp;
file.open(filename.c_str(), ios::in); 打开文件
while(getline(file, tmp, '\n')){ 按行读取
content.append(tmp); 字符串拼接
}
file.close();

file.open(filename.c_str(), ios::out);
cout<<content<<endl;

file<<content; 写入文件
file.close();

return 0;
谢谢,明白了
yiyefangzhou24 2018-09-20
  • 打赏
  • 举报
回复
建议使用ini文件,ini文件的格式是
索引=值
的形式,能很好的切合你数组的格式要求,函数参见
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
)


DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
)

沐雨青城 2018-09-19
  • 打赏
  • 举报
回复
用文件流就可以了
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream file;
string filename = "test.txt";
string content, tmp;
file.open(filename.c_str(), ios::in); 打开文件
while(getline(file, tmp, '\n')){ 按行读取
content.append(tmp); 字符串拼接
}
file.close();

file.open(filename.c_str(), ios::out);
cout<<content<<endl;

file<<content; 写入文件
file.close();

return 0;
棉猴 2018-09-19
  • 打赏
  • 举报
回复
用ifstream类就可以吧?
幻夢之葉 2018-09-19
  • 打赏
  • 举报
回复
搜索 C++文件读写 相关资料

64,439

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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