Zum Inhalt springen

Empfohlene Beiträge

Geschrieben

Hi Leute,

ich will einen char - Array in eine von mir selbst erstellte Datei einfügen. In diese Datei sollen mehrere Arrays hintereinander eingefügt werden. Die Befehle fgets, fwrite und fopen kenn ich schon, weiß aber nicht, wie ich sie richtig benutze.

Danke für eure Hilfe...

Geschrieben

??? Wie Du kennst die Funktionen, kannst sie aber nicht anwenden? Aber egal, wenn Du eh in C++ programmierst, dann solltest Du die Typen ifstream und ofstream benutzen...

Zum Beispiel:

ofstream out ("test.txt");

string txt ="hallo welt";

out << txt;

HTH

Jan

Geschrieben

Das ist schlecht!

Nun, dann gebe ich Dir hier die Information, wie man ifstream und ofstream benutzt. Ließ es Dir genau durch, und wenn Du dann noch eine kronkrete Frage hast, frag!

ifstream

ifstream

#include <fstream.h>

The ifstream class is an istream derivative specialized for disk file input. Its constructors automatically create and attach a filebuf buffer object.

The filebuf class documentation describes the get and put areas and their associated pointers. Only the get area and the get pointer are active for the ifstream class.

Construction/Destruction — Public Members

ifstream

Constructs an ifstream object.

~ifstream

Destroys an ifstream object.

Operations — Public Members

open

Opens a file and attaches it to the filebuf object and thus to the stream.

close

Closes the stream’s file.

setbuf

Associates the specified reserve area to the stream’s filebuf object.

setmode

Sets the stream’s mode to binary or text.

attach

Attaches the stream (through the filebuf object) to an open file.

Status/Information — Public Members

rdbuf

Gets the stream’s filebuf object.

fd

Returns the file descriptor associated with the stream.

is_open

Tests whether the stream’s file is open.

ofstream

#include <fstream.h>

The ofstream class is an ostream derivative specialized for disk file output. All of its constructors automatically create and associate a filebuf buffer object.

The filebuf class documentation describes the get and put areas and their associated pointers. Only the put area and the put pointer are active for the ofstream class.

Construction/Destruction — Public Members

ofstream

Constructs an ofstream object.

~ofstream

Destroys an ofstream object.

Operations — Public Members

open

Opens a file and attaches it to the filebuf object and thus to the stream.

close

Flushes any waiting output and closes the stream’s file.

setbuf

Associates the specified reserve area to the stream’s filebuf object.

setmode

Sets the stream’s mode to binary or text.

attach

Attaches the stream (through the filebuf object) to an open file.

Status/Information — Public Members

rdbuf

Gets the stream’s filebuf object.

fd

Returns the file descriptor associated with the stream.

is_open

Tests whether the stream’s file is open.

Beispiel:

char szBuffer[1000]={NULL};

int nCharacter=0;

ifstream Test("c:\Test.txt", ios::in);

ofstream TestTemp("c:\TestTemp.txt", ios::out);

do

{

nCharacter = Test.get();

TestTemp << nCharacter << "\n";

}while(nCharacter != EOF)

TestTemp.close();

Test.close();

remove("c:\Test.txt");

rename("c:\TestTemp.txt", "c:\Test.txt");

In diesem Beispiel habe ich die Datei Test.txt zum lesen geschaffen und gleichzeitig geöffnet, die Datei TestTemp.txt zum schreiben. Nun lese ich Buchstabe für Buchstabe aus der Test.txt aus, und schreibe jeden Buchstabe einzeln in die TestTemp.txt und hänge dahinter jeweils einen Zeilenumbruch.

Am Ende schließe ich die Dateien wieder, schmeiße die Test.txt weg, und nenne die TestTemp.txt in Test.txt um!

Bine

Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren

Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können

Benutzerkonto erstellen

Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!

Neues Benutzerkonto erstellen

Anmelden

Du hast bereits ein Benutzerkonto? Melde Dich hier an.

Jetzt anmelden

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...