我正在尝试将一些数据输出到.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();
时间和力(N / m)分别在A列和B列中,但t和F值都打印在第一行。
将它们分开打印到A列和F列到B列的语法是什么?
CSV文件没有什么特别之处。您只需按照basic rules即可使用文本编辑器创建它们。 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;
}
这将导致在MS Excel中打开时看起来像这样的CSV文件:
更改
Morison_File << t; //Printing to file
Morison_File << F;
至
Morison_File << t << ";" << F << endl; //Printing to file
a,也可以代替;
这是类似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;
}
文件“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;
}
最新版本here
你必须 ”;” separator,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 << ";" << F;
}
Morison_File.close();
如果您使用C ++编写.csv文件 - 您应该使用以下语法:
myfile <<" %s; %s; %d", string1, string2, double1 <<endl;
这会将三个变量(字符串1和2以及double1)写入单独的列中,并在它们下面留一个空行。在excel中;表示新行,所以如果你想要换一个新行 - 你可以写一个简单的“;”在将新数据写入文件之前。如果你不想在下面有一个空行 - 你应该删除endl并使用:
myfile.open("result.csv", std::ios::out | std::ios::app);
打开.csv文件时的语法(例如result.csv)。这样,下次在result.csv文件中写入内容时 - 它会将其写入最后一行下方的新行 - 因此,如果您愿意,可以轻松管理for循环。