@dandanger: das ist unsinnig, wenn das ganze in eine datei geschrieben wird, da ist es dann umso schwere das ganze wieder auszulesen...
ich habe das mal ausprobiert, indem ich einfach eine zeile in der datei pro zelle in der tabelle genommen habe:
array_write.cpp:
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct eine_zeile
{
string ein_string;
int ein_int;
long ein_long;
};
int main()
{
int y;
int zeilen;
ofstream datei;
string tmp;
cout << "Zeilen: ";
cin >> zeilen;
cout << endl;
getline(cin,tmp); //Buffer leeren - geht bestimmt mit irgendeinem anderen Befehl besser =)
struct eine_zeile eingaben[zeilen];
for(y=0;y<zeilen;y++)
{
cout << endl << "Zeile " << y+1 << ":" << endl;
cout << "ein_string: ";
getline(cin,eingaben[y].ein_string);
cout << "ein_int: ";
cin >> eingaben[y].ein_int;
cout << "ein_long: ";
cin >> eingaben[y].ein_long;
getline(cin,tmp); //Buffer leeren - geht bestimmt mit irgendeinem anderen Befehl besser =)
}
cout << endl;
cout << "Speichere Array..." << endl;
cout << "Zeilen: " << zeilen << endl;
cout << "sizeof(array): " << sizeof(eingaben) << endl;
datei.open("array");
datei << zeilen << endl;
for(y=0;y<zeilen;y++)
{
datei << eingaben[y].ein_string << endl;
datei << eingaben[y].ein_int << endl;
datei << eingaben[y].ein_long << endl;
}
datei.close();
}
und zum auslesen:
array_read.cpp:
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct eine_zeile
{
string ein_string;
int ein_int;
long ein_long;
};
int main()
{
int y;
int zeilen;
ifstream datei;
cout << "Lese Array...";
datei.open("array");
datei >> zeilen;
cout << "Zeilen: " << zeilen << endl;
struct eine_zeile eingaben[zeilen];
cout << "sizeof(array): " << sizeof(eingaben) << endl;
for(y=0;y<zeilen;y++)
{
cout << endl << "Zeile " << y+1 << ":" << endl;
datei >> eingaben[y].ein_string;
cout << "ein_string: " << eingaben[y].ein_string << endl;
datei >> eingaben[y].ein_int;
cout << "ein_int: " << eingaben[y].ein_int << endl;
datei >> eingaben[y].ein_long;
cout << "ein_long: " << eingaben[y].ein_long << endl;
}
datei.close();
}
Lesezeichen