0

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
Schütze
  • 1,044
  • 5
  • 28
  • 48
  • Please find out at which line of your code the "Invalid parameter passed to C runtime function." error message is displayed. And also check if `smartcutLogFile` is `NULL` right after `fopen`. Be aware that `fopen` can fail. – Jabberwocky Jan 15 '18 at 12:33
  • Could it be something related to admin rights? I am trying to create the file under a folder which is under C: directly. – Schütze Jan 15 '18 at 12:38
  • 1
    @Schütze: You tagged the language as C++ but the APIs you are using is of C. And, you are using Qt too! Why don't you simply use `QFile` to do this? You won't be doing those conversions from `QString` to `std::string` or C-strings. – Azeem Jan 15 '18 at 12:42
  • Yes it could. Does `fopen` return `NULL`? Are you able to create a file in that location manually with the explorer? Try putting your file into another location. If `fopen` returns `NULL` what does `errno` contain? Read the `fopen` documentation. – Jabberwocky Jan 15 '18 at 12:42
  • @Azeem I also have qt tag along with C++ under the question. And you are more than welcome to offer an alternative if that is going to solve the question. – Schütze Jan 15 '18 at 12:44
  • @Schütze: In addition to my previous comment, you could do this using Qt logging features. Take a look at `qInstallMsgHandler()`. Here's an example from SO's answer (https://stackoverflow.com/a/4954188/7670262). – Azeem Jan 15 '18 at 12:45
  • 1
    what is the filename that you pass to fopen function. I can see you don't use format string here, and you use format string when you print filename. So maybe filename is different. – Sandro Jan 15 '18 at 12:46
  • @Schütze: Right. That's why I asked to determine whether using C APIs is a compulsion for you. If you are good to go with Qt then you must. It'll make your life easier! ;) – Azeem Jan 15 '18 at 12:47
  • @Sandro Forget the printing part since that is not the part prompting the error. The filename is the current datetime in `char*[]` form after the conversions, the error comes from `fopen()` function. Somewhere in that line the error lies. – Schütze Jan 15 '18 at 12:49
  • 2
    @Schütze Yes error comes from fopen function Maybe the reason is invalid filename. That is why I ask you to check that result of your date/time conversion is correct and gives the valid filename. – Sandro Jan 15 '18 at 12:55
  • @Sandro You were actually right. The formatting was missing. Add your answer and I'll gladly accept it :) – Schütze Jan 15 '18 at 12:59

1 Answers1

2

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()
Schütze
  • 1,044
  • 5
  • 28
  • 48
Sandro
  • 2,707
  • 2
  • 20
  • 30