Datei als Argument in Funktion auf Existenz prüfen
-
Ich bin noch relativ unbewandert in C++, aber ich hoffe Ihr könnt mir weiterhelfen.
Ich habe hier einen Beispiel-Code, den ich zur Existenzprüfung einer Datei verwende. Es wir nur True oder False zurückgegeben.
Ich möchte den Code allerdings universeller haben, so dass ich nur eine gewünschte Datei als Argument einbaue, etwa so:bool FileExist( "/var/beispiel.txt" )Hier das Original:
bool FileExist( void ) { // pruefe Existenz einer Datei if( (access( "/var/beispiel.txt", 0 )) != -1 ) return true; else return false; }Ich komme da leider nicht recht weiter.
Im Voraus Danke!
-
wo kommst du nicht weiter? Lies vielleicht mal ein Tutorial
-
Dieser Thread wurde von Moderator/in kingruedi aus dem Forum Linux/Unix in das Forum C++ verschoben.
Im Zweifelsfall bitte auch folgende Hinweise beachten:
C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?Dieses Posting wurde automatisch erzeugt.
-
Hallo
Du kannst auch boost benutzen, da gibt es eine Funktion mit dem Namen exists, die überprüft, ob die übergebene Datei existiert.
chrische
-
So hatte ich das zuletzt gemacht, aber das will nicht:
Fehlermeldung: [quote]expected primary-expression before "path"boost kenne ich noch nicht!
#include <stdio.h> #include <iostream> #include <fstream> #include <string> using namespace std; bool FileExist(string); bool FileExist(string path) { if( (access( string path, 0 )) != -1 ) return true; else return false; } . . . .
-
Hallo
if( (access(path, 0 )) != -1 )bis bald
akari
-
Danke für die schnelle Antwort.
Ich habs mal getestet:#include <stdio.h> #include <iostream> #include <fstream> #include <string> using namespace std; bool FileExist(string); bool FileExist(string path) { if( (access( path, 0 )) != -1 ) return true; else return false; }Ich bekomme aber aber dann diese Fehlermeldung:
cannot convert 'std::string' to 'const char*' for argument '1' to 'int access(const char*,int)'
-
Hallo
mach dich unbedingt in einem tutorial erstmal mit den C++ grundlagen vertraut
if( (access( path.c_str(), 0 )) != -1 )bis bald
akari
-
Danke hat geklappt!
Durch Tutorials wühle ich mich momentan schon durch, und das wird wohl auch noch einige Zeit in Anspruch nehmen.