C++输出CSV文件(最简单的方式)


参考之前写的输出文件博客,https://blog.csdn.net/qq_42138448/article/details/110307984

总结得下列程序:

#include <iostream>
#include <fstream>
int main( int argc, char* argv[] )
{
      std::ofstream myfile;
      myfile.open ("example.csv");
      myfile << "This is the first cell in the first column.\n";
      myfile << "a,b,c,\n";
      myfile << "c,s,v,\n";
      myfile << "1,2,3.456\n";
      myfile << "semi;colon";
      myfile.close();
      return 0;
}

输出结果:

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Visual Studio 2022中,你可以使用C++来读取CSV文件。以下是一个简单的示例代码: ```cpp #include <iostream> #include <fstream> #include <string> #include <vector> // CSV文件路径 std::string csvFilePath = "C:\\path\\to\\your\\csv\\file.csv"; int main() { std::ifstream file(csvFilePath); // 检查文件是否成功打开 if (!file.is_open()) { std::cout << "Unable to open the file." << std::endl; return 1; } std::vector<std::vector<std::string>> data; std::string line; while (std::getline(file, line)) { std::vector<std::string> row; size_t pos = 0; std::string token; // 使用逗号分隔行数据的字段 while ((pos = line.find(',')) != std::string::npos) { token = line.substr(0, pos); row.push_back(token); line.erase(0, pos + 1); } // 添加最后一个字段 row.push_back(line); // 将分隔后的行数据添加到数据向量中 data.push_back(row); } // 输出读取的数据 for (const auto& row : data) { for (const auto& field : row) { std::cout << field << " "; } std::cout << std::endl; } file.close(); return 0; } ``` 请确保将`"C:\\path\\to\\your\\csv\\file.csv"`替换为你实际的CSV文件路径。此代码将逐行读取CSV文件,并按照逗号分隔每一行的字段,然后将读取的数据存储在一个二维向量中,最后输出读取的数据。 请注意,这只是一个简单的示例代码,没有进行错误处理和数据验证。在实际应用中,你可能需要根据CSV文件的具体格式和需求进行适当的处理和验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值