fstream默认不支持中文路径和输出整数带逗号的解决办法

C++中文路径支持

今天项目中打日志,发现带中文路径名就不能成功创建,经网上了解,发现c++的一套文件IO库,默认是不支持中文路径的。

下面我们用fstream来创建一个新文件,如果文件路径中带有中文,则创建一般会失败。如下面代码:

  1. #include <iostream>  
  2. #include <fstream>  
  3. #include <string>  
  4. #include <direct.h>  
  5.   
  6. using namespace std;  
  7.   
  8. void main()  
  9.  
  10.     _mkdir("测试");      //新建一个中文文件夹  
  11.     ofstream outfile( "测试/test.txt"ios::out );     //创建文件  
  12.     if!outfile  
  13.      
  14.         cout << "Failed to create file!" 
  15.         return  
  16.      
  17.         outfile.close();  
  18.  

程序将输出创建文件夹失败的信息。

一个解决办法是:中文操作系统下,调用locale::global(std::locale("")),将全局区域设置为中文,如下例:

  1. #include <iostream>  
  2. #include <fstream>  
  3. #include <string>  
  4. #include <direct.h>  
  5.   
  6. using namespace std;  
  7.   
  8. void main()  
  9.  
  10.     locale::global(std::locale(""));     //将全局区域设为操作系统默认区域,以支持中文路径  
  11.     _mkdir("测试");      //新建一个中文文件夹  
  12.     ofstream outfile( "测试/test.txt"ios::out );     //创建文件  
  13.     if!outfile  
  14.      
  15.         cout << "Failed to create file!" 
  16.         return  
  17.      
  18.     int 123456789;  
  19.     outfile << "i " << << "/n"  //输出带逗号  
  20.          outfile.close();  
  21.     setlocale( LC_ALL, "C" );     //恢复全局locale,如果不恢复,可能导致cout无法输出中  
  22.  

      创建文件成功,在程序的“测试”文件下出现个test.txt文件。需要注意的是最后需要调用setlocale( LC_ALL, "C" )还原,否则会出现未知错误。 

      此时我们发现,test.txt中的i值显示为123,456,789,出现了逗号。这是因为中文习惯问题。我们在读取文件的时候,读入i值时,可能读入一个值为123的整数,并不是我们希望的123456789,所以我们写文件时,希望不要写入逗号。一个办法是现将整数转换为字符串再进行输出,如:

  1. int 123456789;  
  2. char ch[20];  
  3. sprintf((char *)&ch, "%d"i);    //整数数据转换为字符数组。  
  4. outfile << "i "  << ch << '/n' //输出不带逗号  

      上述问题的解决方法有很多,大家可以试试。

#include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string> #include <regex> struct Record { std::string timestamp; int V; int C; int L_Pump; int R_Pump; }; // 安全字符串转整数函数 int safeStoi(const std::string& str) { try { return std::stoi(str); } catch (...) { return 0; } } int main() { std::ifstream inputFile("C:\\Users\\A\\Desktop\\input.txt"); std::ofstream outputFile("C:\\Users\\A\\Desktop\\output.csv");// 输出CSV文件路径 if (!inputFile.is_open()) { std::cerr << "无法打开输入文件!" << std::endl; return 1; } if (!outputFile.is_open()) { std::cerr << "无法创建输出文件!" << std::endl; return 1; } std::string line; std::regex timestampRegex(R"($$(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$$)"); std::regex fieldRegex(R"((\w+):(\d+\.?\d*))"); // 匹配 Key:Value 格式(支持小数) std::vector<Record> records; Record currentRecord; bool hasValidData = false; while (std::getline(inputFile, line)) { std::smatch match; // 匹配时间戳 if (std::regex_search(line, match, timestampRegex)) { currentRecord.timestamp = match[1]; hasValidData = false; // 开始新记录 } // 匹配字段 std::string::const_iterator searchStart(line.cbegin()); while (std::regex_search(searchStart, line.cend(), match, fieldRegex)) { std::string key = match[1]; std::string value = match[2]; if (key == "V") { currentRecord.V = safeStoi(value); hasValidData = true; } else if (key == "C") { currentRecord.C = safeStoi(value); hasValidData = true; } else if (key == "L_Pump") { currentRecord.L_Pump = safeStoi(value); hasValidData = true; } else if (key == "R_Pump") { currentRecord.R_Pump = safeStoi(value); hasValidData = true; } searchStart = match.suffix().first; } // 如果当前记录包含有效数据,则保存 if (hasValidData && !currentRecord.timestamp.empty()) { records.push_back(currentRecord); std::cout << "添加记录: " << currentRecord.timestamp << std::endl; } } inputFile.close(); // 写入表头 outputFile << "Timestamp,V,C,L_Pump,R_Pump\n"; // 写入数据 for (const auto& rec : records) { outputFile << rec.timestamp << "," << rec.V << "," << rec.C << "," << rec.L_Pump << "," << rec.R_Pump << "\n"; } outputFile.flush(); outputFile.close(); std::cout << "表格文件已成功生成!" << std::endl; return 0; } 文件只记录了表头,没有其他内容
06-30
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值