如何使用sting参数调用函数

时间:2016-04-21 11:21:47

标签: c++

我正面临着我正在处理的代码的问题。每次让我们说条件满足时,我想写一个.txt文件。但是,当我运行附加了此特定代码的模拟时,不会创建任何文件。我想要你对函数定义以及我如何调用它的看法。以下是该函数的定义:

void writeToTxt(const std::string filename, int dir, double XPosTargetlane, double accelcurrent, double accelfollower, double accelaftproj, double acceltoldfollowerafterlanechange, double acceltargetcurrent, double acceltarget, int targetLane, double VelMobil, double currTime, int id_veh){
char buffer[500];
int n;
FILE* fMobil = fopen(filename.c_str(), "a");
if (fMobil != NULL){
    //Initialize output file
    n = sprintf(buffer, "\n  \ XPos \ accelc \ accelf \ accela \ accelto \ acceltar \ acceltarg \ dir \ tar \ Vel \  time \   Polfact=1.0 \ Δath=1.0 ");
    fputs(buffer, fMobil);

    //Store converted value to buffer and append to output file
    n = sprintf(buffer, "\n%.3f\t", XPosTargetlane);
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3f\t", accelcurrent);
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3f\t", accelfollower);
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3f\t", accelaftproj);
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3f\t", acceltoldfollowerafterlanechange);
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3f\t", acceltargetcurrent);          //an*
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3f\t", acceltarget);             //an
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3d\t", dir);
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3d\t", targetLane);
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3f\t", VelMobil);
    fputs(buffer, fMobil);

    n = sprintf(buffer, "%.3f\t", currTime);
    fputs(buffer, fMobil);

    n = sprintf(buffer, "\n-----------------------------------------------------------------------------%d", id_veh);
    fputs(buffer, fMobil);

    fclose(fMobil);
}

}

以下是我的称呼方式:

targetLane2 = numLane - 1;
    XPosTargetlane2 = vehicle->getPositionInTargetlane(vehicle->getPosition(0), targetLane2);
    vehicle->getUpDown(1, XPosTargetlane2, pVehUp, ShiftUp, pVehDw, ShiftDw);
    if ((simVehicleParticular *)vehicle->getRealLeader(temp) != NULL){
        accelcurrent2 = getIDMDeceleration((simVehicleParticular *)vehicle, (simVehicleParticular*)vehicle->getRealLeader(temp));    // ac
    }
    if ((simVehicleParticular *)vehicle->getRealFollower(temp) != NULL){
        accelfollower2 = getIDMDeceleration((simVehicleParticular *)vehicle->getRealFollower(temp), (simVehicleParticular *)vehicle);  //a0
    }
    if (pVehUp != NULL){
        accelaftproj2 = getIDMDeceleration((simVehicleParticular *)vehicle, (simVehicleParticular *)pVehUp);                  //ac*
    }
    if ((simVehicleParticular *)vehicle->getRealFollower(temp) != NULL){
        acceltoldfollowerafterlanechange2 = getIDMDeceleration((simVehicleParticular *)vehicle->getRealFollower(temp), (simVehicleParticular *)vehicle->getRealLeader(temp)); //a0*
    }
    if (pVehDw != NULL && pVehUp != NULL){
        acceltarget2 = getIDMDeceleration((simVehicleParticular*)pVehDw, (simVehicleParticular*)pVehUp);  //an
    }
    if (pVehDw != NULL){
        acceltargetcurrent2 = getIDMDeceleration((simVehicleParticular*)pVehDw, (simVehicleParticular*)vehicle);
    }


writeToTxt("Mobil114.txt", -1, XPosTargetlane2, accelcurrent2, accelfollower2, accelaftproj2, acceltoldfollowerafterlanechange2, acceltargetcurrent2, acceltarget2, targetLane2, VelMobil, currTime, id_veh);

你有什么想法,为什么即使它正确构建,它也不会写一个.txt文件?我没有包含任何标题,因为没有意义。问题出在函数定义或我调用它的某个地方。谢谢。

1 个答案:

答案 0 :(得分:0)

首先,我会调整一些调试语句,以确保代码实际进入if语句(如CompuChip建议的那样)。

其次,如果输入代码,我会确保检查工作目录(如Joachim Pileborg建议的那样),但文件可能是 正在创建,但可能不在您期望的位置