Speichern und Laden von Textdateien

Bismark

Erfahrenes Mitglied
Hi,

ich habe ein Problem mit dem Speichern und Laden von Textdateien.

Ich habe die nötigen Codes geschrieben, damit die Daten die ich während des laufenden Programms eingeben habe, gespeichert und beim öffnen des Programms wieder zu Verfügung stehen, was aber nicht geschieht.

Ich habe die Stellen Markiert.

Ich Hoffe ihr könnt mir helfen.

Code:
#include <conio.h>
#include <iostream>
#include <fstream>
#include <string>
#include "conioex.h"


using namespace std;



struct konto				
{
	string name;
	int kontonr;
	float kbetrag;
};

const int anz = 10;
konto liste[anz];

void kontoalg (konto liste[anz]);
void ausgeben (konto liste[anz]);


int main()													Hauptprogramm
{
char wahl;


	do
	{
	system ("cls");
		cout<<"#### M E N U E ####"<<endl<<endl;
		cout<<"<1> Konto Anlegen"<<endl;
		cout<<"<2> Daten Ausgeben"<<endl;
		cout<<"<#> Programm beenden"<<endl;
		cout<<"Ihre Wahl ==> ";
		cin>>wahl;


		switch (wahl)
		{
		case '1': kontoalg (liste);break;
		case '2': ausgeben (liste);break;
		}
	}
	while(wahl != '#');

getch();

return 0;
}																				


void kontoalg(konto liste[anz])								
{
	char antwort;
	int zae=0, z=0;
	bool merker;


  
	do
	{
	system ("cls");
	gotoxy(29,10);
	cout<<"## KONTO ANLEGEN ##"<<endl<<endl;
	cout<<"Nachname: ";
        gotoxy(29,12);
	cin>>liste[zae].name;
	gotoxy(29,13);
	cout<<"Kontonr.: ";
	cin>>liste[zae].kontonr;
	gotoxy(29,14);
	cout<<"Kontostand: ";
	cin>>liste[zae].kbetrag;

	ofstream dataus;                                                 Speichern
	dataus.open("konten.txt", ios::out);
	
	for(int y=0;y <anz;y++)
	{
		dataus<<liste[y].name<<" ";
		dataus<<liste[y].kontonr<<" ";
		dataus<<liste[y].kbetrag<<endl;
	}
	dataus.close();                                                     //Ende Speichern

	ifstream datin;                         //Anfang Laden
	datin.open("konten.txt", ios::in);
	
	for(int y=0;!datin.eof();y++)
	{
	datin>>liste[y].name<<" ";
	datin>>liste[y].kontonr<<" ";
	datin>>liste[y].kbetrag<<endl;
	}
	datin.close();                                                      //Ende Laden


	zae++;
	system ("cls");
	gotoxy(25,13);
	cout<<"Noch ein Konto anlegen? -j,J/n,N";
	cin>>antwort;


	}
	while(antwort == 'j' || antwort == 'J' && zae < 10 );

}
 
Zuletzt bearbeitet:
Das das kompiliert ...
C++:
{
    std::ofstream file_stream_save("konten.txt");      
    if (!file_stream_save) return false;
	
    // hier besser operator << für "konto" überladen, sonst so:
    const konto* ptr_end(konto + anz);
    for (konto* ptr_it(konto); ptr_it != ptr_end; ++ptr_it) file_stream_save << ptr_it->name << " " << ptr_it->kontonr << " " << ptr_it->kbetrag << std::endl;
} 
{
    std::ifstream file_stream_read("konten.txt"); 

    for (konto* ptr_it(konto); file_stream_read; ++ptr_it) file_stream_read >> ptr_it->name >> ptr_it->kontonr >> ptr_it->kbetrag;
}
 
Zurück