I have a problem with my saveing to a file.. I can save it but it gets saved as numbers, and strange signs..
here is the program, a little long tho.
Code:
#include <conio>
#include <iostream>
#include <stdlib>
#include <fstream>
#include <string>
#include <errno>
#include <dir>
#include <io>
#include <sstream>
using namespace std;
int const ant=2;
void save();
void load();
void meny();
struct Person
{
char fornavn[31], etternavn[31];
int alder, arslonn;
};
void error1()
{
cout << "\nFile do not exist!!!";
getch();
load();
}
void error()
{
cout << "\n!!ERROR!! make sure your filname is under 20 char. !!ERROR!!";
getch();
return;
}
void Lesinn (Person P[])
{
for (int i=0;i<ant;i++)
{
cout << "\nSkriv inn den ansattes fornavn: ";
cin >> P[i].fornavn;
cout << "\nskriv inn den ansattes etternavn: ";
cin >> P[i].etternavn;
cout << "\nskriv inn den ansattes alder: ";
cin >> P[i].alder;
cout << "\nskriv inn den ansattes \x86rslonn: ";
cin >> P[i].arslonn;
}
meny();
}
void save(Person P[])
{
ofstream fil;
char filename[17];
clrscr();
system("dir /b *.sav");
cout << "\n\n";
cout << "Name of savefile? : ";
cin >> filename;
int handle;
handle = creatnew(strcat(filename, ".sav"), 0);
if (handle == -1)
{
cout << "\nFilen eksisterer!";
cout << "\nOverskrive?[y/n] ";
char valg21 = getch();
if(valg21 == 'n')
{
save(P);
}//if
}//if
fil.open(filename);
if (!fil) {error();}
else
{
for (int i=0;i<ant;i++)
{
fil << P[i].fornavn << endl;
fil << P[i].etternavn << endl;
fil << P[i].alder << endl;
fil << P[i].arslonn << endl;
fil << endl;
}
}
fil.close();
meny();
}
void load()
{
clrscr();
ifstream fil;
char filnamn[17];
system("dir /b *.sav");
cout << "\n\n";
cout << "Filen du vil loade [filnavn]? ";
cin >> filnamn;
strcat(filnamn, ".sav");
fil.open(filnamn);
if (!fil) {error1();}
else
{
cout << endl;
char temp[100];
for (int i=0;i<ant;i++)
{
cout << "\nAnsatt nr. " << i << ".\n";
fil >> temp;
cout << "Den annsattes navn er: " << temp;
fil >> temp;
cout << " " << temp << "\n";
fil >> temp;
cout << "Den annsattes alder er: " << temp << "\n";
fil >> temp;
cout << "Den annsattes \x86rslonn er: " << temp << "\n";
}
}
fil.close();
getch();
meny();
}
void process(stringstream& input)
{
Person Ansatt[ant];
string temp;
input >> temp;
if( temp.length() && temp == "load" ) load();
else if( temp.length() && temp == "save" ) save(Ansatt);
else if( temp.length() && temp == "quit") exit(1);
else if( temp.length() && temp == "new") Lesinn(Ansatt);
}
void meny()
{
string command;
clrscr();
gotoxy (5,4); textattr(4 << 0); cprintf("Meny Valg:");
gotoxy (5,7); textattr(8 << 0); cprintf("\x86pne tidligere lagret fil (load).");
gotoxy (5,8); textattr(8 << 0); cprintf("Skrive inn ny fil (new).");
gotoxy (5,9); textattr(8 << 0); cprintf("Lagre filen (save).");
gotoxy (5,10); textattr(8 << 0); cprintf("Lukke programmet (quit).");
gotoxy (5,12); textattr(9 << 0); cprintf("Ditt valg?: ");
getline(cin,command);
while( command.length() )
{
process(stringstream(command));
gotoxy (5,12); textattr(9 << 0); cprintf("Ditt valg?: ");
getline(cin,command);
}
}
main()
{
mkdir("save");
chdir("save");
meny();
}
and I was wondring if there was maybe a better and more compact way of making the same program?
thanks.