• 写csv相关操作


    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();
    	}
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    
    
    • 1
  • 相关阅读:
    熬夜也要肝完的阿里内部面试官手册,吃透直接拿下大厂心仪 offer
    【Flutter】使用Android Studio 创建第一个flutter应用。
    unity基础之协程
    ssm框架
    从零学算法(剑指 Offer 33)
    【PCBA方案设计】握力计方案
    python requests之charles代理配置
    vue3 使用 vite 构建的项目打包后无法访问
    Go语言Web开发Echo框架搭建
    Go map发生内存泄漏解决方法
  • 原文地址:https://blog.csdn.net/weixin_51287642/article/details/133813806