Hallo!
Da ich noch nicht so ganz firm in Sachen C/C++ bin habe ich mal wieder eine
Frage.
Ich versuche gerade folgende Funktion in C nach C++ zu übersetzen in
Verbindung mit QT-Klassen.
char* mk_recurse_dir(char *dir) {
// this will create a directory by that name, then make an file inside
that dir.
int x;
char *out_name;
if (D_MKDIR (dir)) {
if (errno != EEXIST) { // not an error because it exists
x = errno;
DIE(("mk_recurse_dir: Cannot create directory %s: %s\n", dir,
strerror(x)));
}
}
if (chdir (dir)) {
x = errno;
DIE(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir,
strerror(x)));
}
out_name = malloc(strlen("config")+1);
strcpy(out_name, "config");
return out_name;
}
Jetzt habe ich mich durch die QT Doku gewühlt und bin zu diesem Ergebnis
gekommen:
#include
#include
#include
#include
#include
#include
#include
#include
char* mk_recurse_dir(char *dir) {
// this will create a directory by that name, then make an file inside
that dir.
int x;
char *out_name;
QDir *directory = new QDir();
if (directory->mkdir(dir)) {
if (errno != directory->exists(dir)) { // not an error because it exists
x = errno;
qWarning(("mk_recurse_dir: Cannot create directory %s: %s\n", dir,
strerror(x)));
}
}
if (directory->cd(dir)) {
x = errno;
qWarning(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir,
strerror(x)));
}
out_name = malloc(strlen("config")+1);
strcpy(out_name, "config");
return out_name;
}
In wie weit das jetzt funktioniert kann ich leider nicht testen, da beim
compilieren folgender Fehler auftritt und ich jetzt nicht mehr weiter weis.
*compiling cnf.cpp (g++)
*cnf.cpp:47: warning: left-hand operand of comma expression has no effect
*cnf.cpp:47: warning: left-hand operand of comma expression has no effect
*cnf.cpp:52: warning: left-hand operand of comma expression has no effect
*cnf.cpp:52: warning: left-hand operand of comma expression has no effect
*cnf.cpp:54: error: invalid conversion from `void*' to `char*'
**** Exited with status: 2 ***
Zeilen 47 und 52 sind jeweils die qWarnings und Zeile 54 ist:
out_name = malloc(strlen("config")+1);
Wäre nett wenn mir da jemand helfen könnte.
Gruß
Frank