std::ios::in:输入模式,表示以读取方式打开流。
std::ios::out:输出模式。
std::ios::app:追加模式,表示以写入方式打开流,并将数据追加到文件末尾而不清除原有内容。
std::ios::trunc:截断模式,表示以写入方式打开流,并清除文件原有内容。
std::ios::binary:二进制模式,以二进制方式打开流。
std::ios::ate:打开流后将文件指针定位到文件末尾。
//写csv 覆盖
if (0) {
std::ofstream outFile;
outFile.open("t444est.csv", std::ios::out | std::ios::trunc);
// 写入标题行
outFile << "name" << ','
<< "income" << ','
<< "expenditure" << ','
<< "addr" << std::endl;
// ********写入两行数据*********
// 写入字符串(数字)
outFile << "zhangsan" << ','
<< "3000" << ','
<< "1200" << ','
<< "中国 北京市" << std::endl;
// 写入浮点数(转为字符串)
outFile << "lisi" << ','
<< std::to_string(2032.1) << ','
<< std::to_string(789.2) << ','
<< "中国 陕西省13333311" << std::endl;
std::string name1="1222222";
std::string name2="哈哈";
std::string name3="asd好";
CString name4;
name4 = _T("号1111sad");
outFile << name1 << ',';
outFile << name2 << ',';
outFile << name3 << ',';
outFile << (CW2A)name4 << std::endl;
outFile.close();
}
//写csv 不覆盖
if (1) {
std::ofstream outFile;
outFile.open("t2est.csv", std::ios::app);
// 写入标题行
int a = 977676;
outFile << "name" << ','
<< "income" << ','
<< "expenditure" << ','
<< "addr" << std::endl;
// ********写入两行数据*********
// 写入字符串(数字)
outFile << "" << ',' << "asfasf"
<< a << ','
<< "1200" << ','
<< "中国";
outFile << "北京asdasfasfsa市" << std::endl;
// 写入浮点数(转为字符串)
outFile << "lisi" << ','
<< std::to_string(2032.1) << ','
<< std::to_string(789.2) << ','
<< "中国 陕西省13333311" << std::endl;
std::string name1 = "1222222";
std::string name2 = "哈哈";
std::string name3 = "asd好";
std::string name4 = "号sad";
outFile << name1 << ',';
outFile << name2 << ',';
outFile << name3 << ',';
outFile << name4 << std::endl;
outFile.close();
}