Thread: some problems with saving to a file.

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    some problems with saving to a file.

    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.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    sorry about the text, some of it is in norwegian.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:54 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. WAV file problems...
    By snerl in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 03:00 PM