Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Dateizugriff aus einer Funktion heraus

Empfohlene Antworten

Veröffentlicht

Hallo,

ich möchte auf eine Datei aus einer Funktion heraus zugreifen, bekomme das alleine jedoch nicht hin (bzw. Frage: geht das überhaupt?). Ich möchte die Datei ungern vorher schließen und dann in der Funktion erneut öffnen, da dies ein willkürliches Speichern beinhalten würde.

Ich öffne die Datei in int main() wie folgt:

FILE *data;

data = fopen("Datei.txt", "r+");

Nun habe ich mir gedacht, muss ich ja nun die Speicheradresse des Dateipointers an die Funktion übergeben, also so:

funktion1( &data );

Jetzt habe ich meine Funktion geschrieben zum Testen:

void funktion1( FILE **funktion_ptr ) {

fclose( *funktion_ptr );

}

Diese Funktioniert jedoch nicht. Die Fehlermeldung ist "incompatible pointer type". Vor lauter Nachdenken darüber, kriege ich es nicht mehr auf die Reihe. Wäre nett, wenn mir jemand helfen könnte.

Vielen Dank für Antworten!

na bei einem pointer ist &pointer die speicheradresse des pointers selbst, pointer ist die speicheradresse dessen worauf der pointer zeigt und *pointer der inhalt dessen worauf er zeigt.

der compiler meckert jedoch alle drei varianten an:

1. funktion1(&data);

2. funktion1(data);

3. funktion1(*data);

Habe es schon selbst herausgefunden:

#include "stdio.h"

#include "stdlib.h"

void schliessfunktion( FILE** data2 ) {

fclose( *data2 );

}

int main() {

FILE* data;

data = fopen("Datei.txt", "r+");

schliessfunktion( &data );

return 0;

}

Danke!

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.