what is keyword i have to use instead snprintf in qt ?
-
I want to implement below function in qt . so i need guide regarding which function of qt i need to choose instead snprintf, printf and exit(1) ?
static char filename[20]; static int adapter_nr = 2; snprintf (filename, 19, "/dev/i2c-%d", adapter_nr); filehandle = open(filename,O_RDWR); if (filehandle < 0) { printf ("Error : Can't open /dev/i2c-%d\n",adapter_nr); /* ERROR HANDLING; you can check errno to see what went wrong */ exit(1); } return 0; -
I want to implement below function in qt . so i need guide regarding which function of qt i need to choose instead snprintf, printf and exit(1) ?
static char filename[20]; static int adapter_nr = 2; snprintf (filename, 19, "/dev/i2c-%d", adapter_nr); filehandle = open(filename,O_RDWR); if (filehandle < 0) { printf ("Error : Can't open /dev/i2c-%d\n",adapter_nr); /* ERROR HANDLING; you can check errno to see what went wrong */ exit(1); } return 0;- printf - std::cout
- exit - https://doc.qt.io/qt-5/qcoreapplication.html#exit
- snprintf - see all the QString::arg methods like https://doc.qt.io/qt-5/qstring.html#arg
- File handling - QFile
-
Hi
instead snprintf, printf and exit(1) ?
-
snprintf = QStrings arg. https://doc.qt.io/qt-5/qstring.html#arg
-
printf = You can still use it if you want. but printing to console in a GUI app is mostly for logging.
For user feed back, you can use https://doc.qt.io/qt-5/qmessagebox.html
TO show info to the end user. -
exit(1); will still work and terminate the app. You might not want to do that for a GUI program and rather
display info to user.
-
-
I want to implement below function in qt . so i need guide regarding which function of qt i need to choose instead snprintf, printf and exit(1) ?
static char filename[20]; static int adapter_nr = 2; snprintf (filename, 19, "/dev/i2c-%d", adapter_nr); filehandle = open(filename,O_RDWR); if (filehandle < 0) { printf ("Error : Can't open /dev/i2c-%d\n",adapter_nr); /* ERROR HANDLING; you can check errno to see what went wrong */ exit(1); } return 0;- printf - std::cout
- exit - https://doc.qt.io/qt-5/qcoreapplication.html#exit
- snprintf - see all the QString::arg methods like https://doc.qt.io/qt-5/qstring.html#arg
- File handling - QFile
-
Hi
instead snprintf, printf and exit(1) ?
-
snprintf = QStrings arg. https://doc.qt.io/qt-5/qstring.html#arg
-
printf = You can still use it if you want. but printing to console in a GUI app is mostly for logging.
For user feed back, you can use https://doc.qt.io/qt-5/qmessagebox.html
TO show info to the end user. -
exit(1); will still work and terminate the app. You might not want to do that for a GUI program and rather
display info to user.
@mrjj Yes i might not want to use exit for an gui program. so i have to use return some value other than 0 to indicate this function failed.
i heard that snprintf stop crash if more than 20 character i write into filename array . will same thing possible with QStrings arg ?
-
-
@mrjj Yes i might not want to use exit for an gui program. so i have to use return some value other than 0 to indicate this function failed.
i heard that snprintf stop crash if more than 20 character i write into filename array . will same thing possible with QStrings arg ?
Hi
@mrjj Yes i might not want to use exit for an gui program. so i have to use return some value other than 0 to indicate this function failed.
I think you dont want to use exit at all but simply return the error code, from errno - maybe so the calling function can know it went wrong.
i heard that snprintf stop crash if more than 20 character i write into filename array . will same thing possible with QStrings arg ?
Well your buffer is filename[20]; so you can overflow it.With QString there is no such buffer directly so most likely it won't be an issue as paths won't be that long anyway.
-
@mrjj Yes i might not want to use exit for an gui program. so i have to use return some value other than 0 to indicate this function failed.
i heard that snprintf stop crash if more than 20 character i write into filename array . will same thing possible with QStrings arg ?
@Qt-embedded-developer said in what is keyword i have to use instead snprintf in qt ?:
i heard that snprintf stop crash if more than 20 character
Do you mean it crashes with < 20 characters?! Where did you here that and which implementation was it? I never heard about QString::arg crashing, so just use it (you can also write tests for your use cases to make sure it isn't crashing).
-
@Qt-embedded-developer said in what is keyword i have to use instead snprintf in qt ?:
i heard that snprintf stop crash if more than 20 character
Do you mean it crashes with < 20 characters?! Where did you here that and which implementation was it? I never heard about QString::arg crashing, so just use it (you can also write tests for your use cases to make sure it isn't crashing).
@jsulm i am telling "more than 20 " means not <20 . its >20.
-
@jsulm i am telling "more than 20 " means not <20 . its >20.
@Qt-embedded-developer Well, you wrote: "i heard that snprintf stop crash if more than 20 character"...
-
@Qt-embedded-developer Well, you wrote: "i heard that snprintf stop crash if more than 20 character"...
@jsulm Sorry for not write clearly. when i write more than 20 characters using sprintf then it make crash program in c. so to avoid that situation. in c for safe side to avoid crash we use snprintf.
-
@jsulm Sorry for not write clearly. when i write more than 20 characters using sprintf then it make crash program in c. so to avoid that situation. in c for safe side to avoid crash we use snprintf.
@Qt-embedded-developer
I think it most unlikely that an implementation ofsnprintf()will "crash" (or go wrong) if more than 20 characters, and I don't know where you think you have heard this. It would have been reported. Sostatic char filename[20]; snprintf (filename, 20, "/dev/i2c-%d", adapter_nr);should be fine.
However this doesn't matter if you use
QStringmethods anyway.