简体   繁体   中英

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. 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.

This should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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