Testen, ob eine Datei existiert, ohne sie vorher zu öffnen



  • Unter Win32 geht es wimni mit GetFileAttributes() gut.



  • @Bashar
    Wenn man sie zum schreiben öffnen will, aber sicher sein will das man nicht eine Datei die schon existiert zerstört. Dann ist es auch wichtig das man weis das sie existiert, auch wenn man sie nicht zugreifen darf. Denn 2 Dateien gleichen Namens machen sich im selben Verzeichnis nicht gut.

    Oder wenn man falls diese Datei existiert sie umbenennen möchte z.b in name.old, damit man anschließend sowohl die alte als auch die neue Version hat.



  • Es ist deswegen schlimm, eine Datei einfach zu öffnen, weil es so umständlich ist.
    Das mit findfirst ist mir gar nicht in den Kopf gekommen, GetFileAttributes finde ich nicht so gut.

    Danke euch allen.

    Unzi



  • PAD schrieb:

    @Bashar
    Wenn man sie zum schreiben öffnen will, aber sicher sein will das man nicht eine Datei die schon existiert zerstört.

    Dann öffnet man die Datei beispielsweise unter Unix mit O_CREAT | O_EXCL (anlegen aber nur wenn nicht existent).

    Windows hat da sicher auch was.

    unzi schrieb:

    Es ist deswegen schlimm, eine Datei einfach zu öffnen, weil es so umständlich ist.

    Inwiefern umständlicher? Ich würd eher sagen einfacher, weil die unnötige Existenzabfrage wegfällt.



  • Standardfall (ANSI) ist

    FILE *fopen( const char *filename, const char *mode );

    The character string mode specifies the type of access requested for the file, as follows:

    "r"

    Opens for reading. If the file does not exist or cannot be found, the fopen call fails.

    "w"

    Opens an empty file for writing. If the given file exists, its contents are destroyed.

    "a"

    Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.

    "r+"

    Opens for both reading and writing. (The file must exist.)

    "w+"

    Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

    "a+"

    Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.

    In der API Leider Platform und OS spezifisch

    HANDLE CreateFile(
    LPCTSTR lpFileName,
    DWORD dwDesiredAccess,
    DWORD dwShareMode,
    LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    DWORD dwCreationDispostion ,
    DWORD dwFlagsAndAttributes,
    HANDLE hTemplateFile);

    dwCreationDispostion
    [in] Specifies which action to take on files that exist, and which action to take when files do not exist. For more information about this parameter, see the Remarks section. This parameter must be one of the following values:
    Value Description
    CREATE_NEW Creates a new file. The function fails if the specified file already exists.
    CREATE_ALWAYS Creates a new file. If the file exists, the function overwrites the file and clears the existing attributes.
    OPEN_EXISTING Opens the file. The function fails if the file does not exist.
    See the Remarks section for a discussion of why you should use the OPEN_EXISTING flag if you are using the CreateFile function for devices, including the console.
    OPEN_ALWAYS Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDisposition were CREATE_NEW.
    TRUNCATE_EXISTING Opens the file. Once opened, the file is truncated so that its size is zero bytes. The calling process must open the file with at least GENERIC_WRITE access. The function fails if the file does not exist.

    😃 Wir suchen doch Standardieserbare Lösungen, oder ? 😃

    ➡ Und fopen ist nicht im geringsten umständlich!!!



  • von boost gibst doch eine filesystemlib http://www.boost.org/libs/filesystem/doc/index.htm



  • PAD schrieb:

    Wir suchen doch Standardieserbare Lösungen, oder ?

    Ja schon, aber es gibt im Standard keine Funktion, die testet, ob eine Datei vorhanden ist. Du kannst versuchen, die Datei zu öffnen. Wenn das fehlschlägt, heißt das aber nicht, dass sie nicht existiert.



  • Ich hätte da noch eine Idee,
    Als erstes öffnen wir die Datei, können wir sie öffnen ok.
    Geht es nicht, können wir mit _creat versuchen sie zu erstellen.
    Da gibt es dann nur die Möglichkeiten, die Datei wird erstellt, sie wird geöffnet (falls rechte vorhanden), oder ein Fehler kommt.

    Das sollte eigentlich auf allen Systemen laufen.



  • @daishi _creat ist OS-Spezifisch, damit 😞 leider 😞 nur genauso intelligent wir createfile.



  • @daishi: Dann kann man genauso gut access benutzen. Ist auch nicht Standard.


Anmelden zum Antworten