Hej alle sammen og godt nytår (hvis det altså ikke er for sent at sige det
)...
Jeg er gået igang med at lave et lille project som skal kunne gemme informationer omkring DVD film i en binær fil som hedder DVDdatabase.dat...
Jeg kan huske at jeg engang så en person som havde lavet en "preformated" fil hvor han havde lavet 1000 records...
Jeg prøver nu selv at lave noget ligende.. jeg har derfor lavet en function som hedder formatDatabase() som "skulle" gå ind og lave en fil med 1000 rene accounts, jeg har så derefter lavet en lille function som hedder newDVD som gerne skulle gå ind og ligge noget i den første account (altså account 0) ...
jeg prøver derefter på at udskrive account 0 bagefter men intet sker...
Her er alt hvad jeg har lavet indtil vidre
DVDLibary.cpp
#include <iostream>
using namespace std;
#include <iomanip>
#include <string>
using std::string;
#include <stdlib.h>
#include<fstream>
using std::ifstream;
using std::ofstream;
using std::fstream;
#include "DVD.h"
char FILE_NAME[]="DVDdatabase.dat";
main()
{
cout << endl;
cout << "Checking for database..." << endl;
cout << endl;
//open for reading and writing!
fstream inOutDatabase(FILE_NAME, ios::in | ios::out);
//exit if can't open:
if (!inOutDatabase)
{
cout << "No database file has been found..." << endl;
int choise = 0;
do
{
cout << "----------------------------------------------------------------" << endl;
cout << " 1 - Exit" << endl;
cout << " 2 - Create new database file" << endl;
cout << "Enter a choise, and press <Enter>: ";
cin >> choise;
} while (choise < 1 || choise > 2);
switch(choise)
{
case 1:
exit(0);
case 2:
// create a new file...
DVD::formatDatabase();
fstream testFile(FILE_NAME);
if (!testFile)
{
cout << "----------------------------------------------------------------" << endl;
cout << "Error creating file... The program will terminate now!" << endl;
cout << "----------------------------------------------------------------" << endl;
exit(1);
}
else
{
cout << "The database has been sucessfully created..." << endl;
} // end if (!inOutSchedule)
testFile.close();
break;
} // end switch
}//end if(!inOutSchedule)
else
{
cout << "Database sucessfully found..." << endl;
}
/***********/
/* creates a test account on position 0 */
/***********/
DVD::newDVD(inOutDatabase);
// should print record 0
DVD::printDVD(inOutDatabase);
return 0;
}
DVD.h
#ifndef DVD_H
#define DVD_H
class DVD {
public:
// default constructor...
DVD::DVD(string = "", // title
int=0, // year
int=0, // serial
string = ""); // page
// accessor for title
void setTitle(string);
string getTitle() const;
// accessor for year
void setYear(int);
int getYear() const;
// accessor for serial
void setSerial(int);
int getSerial() const;
// accessor for page
void setPage(string);
string getPage() const;
static void formatDatabase();
static void newDVD(fstream&);
static void printDVD(fstream&);
private:
string title;
int year;
int serial;
string page;
};//end class definition
#endif
DVD.cpp
#include <iostream>
#include <string>
using namespace std;
using std::string;
#include<fstream>
using std::ifstream;
using std::ofstream;
using std::fstream;
#include <stdlib.h>
#include "DVD.h"
/*********************************************************************/
/* DEFAULT CONSTRUCTOR */
/* string title */
/* int year */
/* int serial */
/* string page */
/*********************************************************************/
DVD::DVD(string theTitle, int theYear, int theSerial, string thePage)
{
setTitle(theTitle);
setYear(theYear);
setSerial(theSerial);
setPage(thePage);
}
/*********************************************************************/
/* setTitle */
/*********************************************************************/
void DVD::setTitle(string theTitle)
{
// copy at the most 50 characters to title
const char *titleValue = theTitle.data();
int length = theTitle.length();
if (length > 50)
{
length = 50;
}
title = theTitle.substr(0, length);
}
/*********************************************************************/
/* getTitle */
/*********************************************************************/
string DVD::getTitle() const
{
return title;
}
/*********************************************************************/
/* setYear */
/*********************************************************************/
void DVD::setYear(int theYear)
{
year = theYear;
}
/*********************************************************************/
/* getYear */
/*********************************************************************/
int DVD::getYear() const
{
return year;
}
/*********************************************************************/
/* setSerial */
/*********************************************************************/
void DVD::setSerial(int theSerial)
{
serial = theSerial;
}
/*********************************************************************/
/* getSerial */
/*********************************************************************/
int DVD::getSerial() const
{
return serial;
}
/*********************************************************************/
/* setPage */
/*********************************************************************/
void DVD::setPage(string thePage)
{
// copy at the most 5 characters to page
const char *pageValue = thePage.data();
int length = thePage.length();
if (length > 5)
{
length = 5;
}
page = thePage.substr(0, length);
}
/*********************************************************************/
/* getPage */
/*********************************************************************/
string DVD::getPage() const
{
return page;
}
/*********************************************************************/
/* formatDatabase */
/*********************************************************************/
void DVD::formatDatabase()
{
system("cls"); // claers the screen...
cout << endl;
char choise;
bool OK = false;
do
{
cout << "Are you sure that you want to format the current file? (Y/N): ";
cin >> choise;
if ((choise == 'Y') || (choise == 'y') || (choise == 'N') || (choise == 'n'))
OK = true;
else
OK = false;
} while (OK == false);
if (choise == 'Y' || choise == 'y')
{
ofstream databaseFile("DVDdatabase.dat", ios::binary);
DVD blank;
for (int i = 0; i < 1000; i++)
{
databaseFile.write(reinterpret_cast<const char *> (&blank), sizeof(DVD));
} // end for
databaseFile.close(); // close the steam!
}
else
{
exit(0);
}
}
void DVD::newDVD(fstream &toDatabase)
{
toDatabase.seekg((0)*sizeof(DVD));
DVD newDVD;
cout << "reading record..." << endl;
toDatabase.read(reinterpret_cast<char *>(&newDVD), sizeof(DVD));
char title[50] = "";
// create it, if it doesn't already exist
cout << "---------------------" << endl;
cout << "Enter title: ";
cin >> title;
// populate account...
newDVD.setTitle(title);
cout << "Writing record..." << endl;
toDatabase.write(reinterpret_cast<const char *> (&newDVD), sizeof(DVD));
}
void DVD::printDVD(fstream &fromDatabase)
{
fromDatabase.seekg((0)*sizeof(DVD));
DVD newDVD2;
fromDatabase << newDVD2.getTitle().data() << endl;
}
Jeg kan ikke selv finde ud af hvor det går galt, måske der er noget vigtigt som jeg har glemt/misforsået omkring lagring af data i en binær fil...
Jeg håber at der er nogen som kan finde fejlen eller måske bare guide mig hen imor en mulig løsning...
På forhånd tak!
Smox
[Redigeret d. 12/01-05 10:38:04 af Simon Rasmussen]