
Geschrieben von
take
(Moderator) am 12.04.2007 um 00:00 Uhr.
Beiträge: 613 /
#1201
Kontakt:
Mailen
|
WWW
#include "stdafx.h"
#include <fstream>
#include <string>
using namespace std;
Includen der benötigten Header
fstream f;
f.open("text.dat", ios::app);
f << o << "|" << a << "
";
f.close();
wir öffnen über f.open unser File und sagen Ihm über "ios::app" das wir am ende weiter schreiben wollen.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
fstream f;
char cstring[256];
f.open("text.dat", ios::in);
while (!f.eof())
{
f.getline(cstring, sizeof(cstring));
cout << cstring << "
";
}
f.close();
Hier lesen wir Zeile für Zeile aus unserer Datei herraus.
Mehr Informationen über "fstream" gibt es unter:
http://www.cplusplus.com/reference/iostream/fstream/
Hier noch einmal ein komplettes Programm:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
#include "stdafx.h"
#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int menu=1;
string o, a;
do
{
cout << "1) Eingabe
2) Ausgabe
3) Beenden
: ";
cin >> menu;
if(menu == 1) {
cout << "Eingabe 1: "; cin >> o;
cout << "Eingabe 2: "; cin >> a;
fstream f;
f.open("text.dat", ios::app);
f << o << "|" << a << "
";
f.close();
system("pause");
}
else if(menu == 2){
fstream f;
char cstring[256];
f.open("text.dat", ios::in);
while (!f.eof())
{
f.getline(cstring, sizeof(cstring));
cout << cstring << "
";
}
f.close();
system("pause");
}
}
while(menu > 0 && menu < 3);
return 0;
}
Viele Grüße
"Jeder, der andere kritisiert, muss eine Alternative haben, die er ihnen anbieten kann." - Mo Ti