从C++编写.csv文件

use*_*758 36 c++ csv

我正在尝试将一些数据输出到.csv文件,并将其输出到文件,但它没有将数据分成不同的列,似乎输出数据不正确.

    ofstream Morison_File ("linear_wave_loading.csv");         //Opening file to print info to
    Morison_File << "Time Force(N/m)" << endl;          //Headings for file
    for (t = 0; t <= 20; t++) {
      u = sin(omega * t);
      du = cos(omega * t); 
      F = (0.5 * rho * C_d * D * u * fabs(u)) + rho * Area * C_m * du; 

      cout << "t = " << t << "\t\tF = " << F << endl;
      Morison_File << t;                                 //Printing to file
      Morison_File << F;

    }

     Morison_File.close();
Run Code Online (Sandbox Code Playgroud)

时间和力(N/m)分别在A列和B列中,但t和F值都打印在第一行.

将它们分开打印到A列和F列到B列的语法是什么?

BHa*_*awk 70

CSV文件没有什么特别之处.只需遵循基本规则,即可使用文本编辑器创建它们.在RFC 4180 (tools.ietf.org/html/rfc4180)接受分离器是逗号""不分号';’.像MS Excel这样的程序希望逗号作为分隔符.

有一些程序,处理逗号作为小数和分号作为分隔符,但这些都是技术上外"接受"标准CSV格式的文件.

因此,在创建CSV时,您需要创建文件流并添加以下行:

#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;
}
Run Code Online (Sandbox Code Playgroud)

这将导致在MS Excel中打开时看起来像这样的CSV文件:

使用C++创建的CSV文件的图像

  • MS Excel将根据用户的区域设置不同地解释CSV文件."欧洲"Excel需要使用分号分隔的字段(并使用逗号作为小数点). (9认同)

Mr.*_*pMe 8

更改

Morison_File << t;                                 //Printing to file
Morison_File << F;
Run Code Online (Sandbox Code Playgroud)

Morison_File << t << ";" << F << endl;                                 //Printing to file
Run Code Online (Sandbox Code Playgroud)

a,也可以代替;


Val*_*ich 6

这是类似STL的类

文件"csvfile.h"

#pragma once

#include <iostream>
#include <fstream>

class csvfile;

inline static csvfile& endrow(csvfile& file);
inline static csvfile& flush(csvfile& file);

class csvfile
{
    std::ofstream fs_;
    const std::string separator_;
public:
    csvfile(const std::string filename, const std::string separator = ";")
        : fs_()
        , separator_(separator)
    {
        fs_.exceptions(std::ios::failbit | std::ios::badbit);
        fs_.open(filename);
    }

    ~csvfile()
    {
        flush();
        fs_.close();
    }

    void flush()
    {
        fs_.flush();
    }

    void endrow()
    {
        fs_ << std::endl;
    }

    csvfile& operator << ( csvfile& (* val)(csvfile&))
    {
        return val(*this);
    }

    csvfile& operator << (const char * val)
    {
        fs_ << '"' << val << '"' << separator_;
        return *this;
    }

    csvfile& operator << (const std::string & val)
    {
        fs_ << '"' << val << '"' << separator_;
        return *this;
    }

    template<typename T>
    csvfile& operator << (const T& val)
    {
        fs_ << val << separator_;
        return *this;
    }
};


inline static csvfile& endrow(csvfile& file)
{
    file.endrow();
    return file;
}

inline static csvfile& flush(csvfile& file)
{
    file.flush();
    return file;
}
Run Code Online (Sandbox Code Playgroud)

文件"main.cpp"

#include "csvfile.h"

int main()
{
    try
    {
        csvfile csv("MyTable.csv"); // throws exceptions!
        // Hearer
        csv << "X" << "VALUE"        << endrow;
        // Data
        csv <<  1  << "String value" << endrow;
        csv <<  2  << 123            << endrow;
        csv <<  3  << 1.f            << endrow;
        csv <<  4  << 1.2            << endrow;
    }
    catch (const std::exception& ex)
    {
        std::cout << "Exception was thrown: " << e.what() << std::endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这里有最新版本

  • 如果需要一个开放的 `std::ostream` 来工作,这个 csv-writer 类会更有帮助,想想 `std::stringbuf` (4认同)