简体   繁体   English

C ++:无效的参数传递给C运行时函数

[英]C++: Invalid parameter passed to C runtime function

I am trying to create a log file with the following piece of code: 我正在尝试使用以下代码创建日志文件:

FILE* smartcutLogFile;

D1 = 0;
D2 = 0;
E2 = 0;
E3 = 0;
E4 = 0;
Z_EDGE = 0;

// save the detected values into the log file, and close it
    smartcutLogFile =  fopen ((QDateTime::currentDateTime().toString() + ".txt").toStdString().c_str() ,"w+t"); // get the datetime and append .txt at the end
    std::cout<<(QDateTime::currentDateTime().toString("yyyy-MM-dd hh.mm.ss") + ".txt").toStdString().c_str()<<std::endl;
    fprintf(smartcutLogFile, "D1: %f\n D2: %f\n E2: %f\n E3: %f\n E4: %f\n Z: %f\n", D1, D2, E2, E3, E4, Z_EDGE);
    fclose(smartcutLogFile);

where all these doubles (E2, E3, etc.) are actually measurements from the sensors which I can see on my LineEdits, so all are OK. 实际上,所有这些双精度值(E2,E3等)都是来自传感器的测量值,我可以在LineEdits上看到它们,因此一切正常。 However the following code does not create any file or anything, it does print the file name as such: 但是,以下代码不会创建任何文件或任何内容,它会像这样打印文件名:

2018-01-15 12.21.50.txt

but it does not create anything, rather prompts the following error for hundreds of times: 但它不会创建任何内容,而是会提示以下错误数百次:

Invalid parameter passed to C runtime function.

Where am I doing wrong? 我在哪里做错了?

EDIT: I get the error at the following line: 编辑:我在以下行得到错误:

smartcutLogFile =  fopen ((QDateTime::currentDateTime().toString() + ".txt").toStdString().c_str() ,"w+t"); // get the datetime and append .txt at the end

Maybe an error occurs during the conversion of date/time to string. 在将日期/时间转换为字符串的过程中可能会发生错误。 The result of conversion gives you invalid filename and it can be the reason of fopen function failure. 转换结果为您提供了无效的文件名,这可能是fopen函数失败的原因。

This should work: 这应该工作:

(QDateTime::currentDateTime().toString("yyyy-MM-dd h.mm.ss") + ".txt").toStdString()).c_str()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM