写入文件c ++的语法

时间:2016-03-25 03:24:48

标签: c++ io fstream

class State{
private:
    double _timestamp; // time stamp at which this state is measured
    double _xpos; // vehicle position, forward (x1)
    double _ypos; // vehicle position, left/right (x2)
    double _tire_angle; // tire angle (radians) (x3)
    double _heading; // heading (radians) (x4)

public:
     // constructs a new State object with these initial values
     State(double x1, double x2, double x3, double x4, double timestamp);
     // constructs an empty State object
     State();

     double getXPos() const; // returns the _xpos
     void setXPos(double xpos); // sets the _xpos
     double getYPos() const; // returns the _ypos
     void setYPos(double ypos); // sets the _ypos
     double getTireAngle() const; // returns the _tire_angle
     void setTireAngle(double angle); // sets the _tire_angle
     double getHeading() const; // gets the _heading
     void setHeading(double heading); // sets the _heading
     double getTimeStamp() const; // gets the _timestamp
     void setTimeStamp(double timestamp); // sets the _timestamp
};


class DataSink{
private:
    vector<State> sink;
public:
    void setSink(State x);
    void printSink(char* fileName);
}; 

void DataSink::printSink(char* fileName){
    int i;
    ofstream outFile;

    outFile.open(fileName);

    if (!outFile.is_open())
    {
        cout << "Could not open file " << fileName << endl;
        return;
     }

    for (i = 0; i < sink.size(); ++i)
    {
        outFile << sink.at(i).getXPos() << "," << sink.at(i).getYPos()<< "," << sink.at(i).getTireAngle()<< "," << sink.at(i).getHeading()<< "," << sink.at(i).getTimeStamp() << endl;
    }

    outFile << "Test.\n";

    outFile.close();

    return;
 }

因此,此代码假定将另一个对象类型的向量接收器复制到文件中。当我逐步执行我的函数时,所有的值都应该是它们的位置,但我写的文件只是空的。我添加了&#34;测试&#34;看看是不是我的矢量/价值被抬高了......但即使那样也不会打印出来。它没有告诉我&#34;无法打开文件&#34;任

提前感谢您的帮助和专业知识。

0 个答案:

没有答案