Netzer82 Geschrieben 13. Juni 2002 Geschrieben 13. Juni 2002 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...
Orffi Geschrieben 13. Juni 2002 Geschrieben 13. Juni 2002 ??? 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
Netzer82 Geschrieben 14. Juni 2002 Autor Geschrieben 14. Juni 2002 Tut mir ja sehr leid, aber ich check das nicht. Was ist da was. Erklär den Code mal bitte ein bisschen.
Woodstock Geschrieben 14. Juni 2002 Geschrieben 14. Juni 2002 In seinem Code schreibt er das was in dem Array steht in die Datei! Mehr nicht. Was für eine Programmierumgebung benutz Du? Mit was lernst Du? Bine
Netzer82 Geschrieben 14. Juni 2002 Autor Geschrieben 14. Juni 2002 Ich benutze Visual C++ 4.0 unter Windows NT 4.0.
Woodstock Geschrieben 14. Juni 2002 Geschrieben 14. Juni 2002 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
Empfohlene Beiträge
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 erstellenAnmelden
Du hast bereits ein Benutzerkonto? Melde Dich hier an.
Jetzt anmelden