向txt文本中写入字符串

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

/* 向txt文本中写入一个字符串:
若文件不存在,则新建一个文件;否则,直接输入内容
*/

int main()
{
ofstream os; //创建一个文件输出流对象
os.open("abc.txt");//将对象与文件关联
string mac = "123";
string Device_ID = "1-1-1-1";
string Salt = "354";
string DPK ="Hello";
string Version = "3";
string title = "mac,Device ID,Salt,DPK,Version";
cout << title << endl;
cout << mac << endl;

//cin >> str;
os << title << endl; //将输入的内容放入txt文件中
os << mac << "," <<Device_ID << "," << Salt << "," << DPK << "," << Version << endl; //将输入的内容放入txt文件中

os.close();

return 0;
}

posted @ 2021-06-24 08:09  江南王小帅  阅读(791)  评论(0)    收藏  举报