i want to create a warehouse program for school using the random access file management.
i built a class called product(prodotto) with many private var and i must create a new record.

The function checks if the product already exists with the seekg pointer and reading the file. After u can write the file in that space if there is not. the key is the product code (codice).

The issue is that every time i run the function the former record is erased.

before this version i used a general fstream to do the work but it was worse. the file.dat was always empty.

i checked the pointers with tellg and tellp and they are ok. also the reading part seems ok. I think the problem is the writing statement.

this is the function:

void aggiungiProd()
{
    int code;
    string name,section, description;
    int quantity;
    double price;


    cout<<"\n(insert 0 to come back)"<<endl;

    //exit condiction

    cout<<"Insert product code:";

cin>>code;


if( cin.good()==0)
{ code=0;
    cout<<"invalid code. Please reinsert:"<<endl;
cin>>code;
} //also this part is not working but is less important


if(codice==0) return;//exit condition



ifstream readFile("prod.dat",ios::in);
if(!readFile){
        cerr<<"Error during the reading";
 exit(1);
    }
int var;
var=code-1;

//use a pointer to indicate the location to read
readFile.seekg(var * (sizeof(Prodotto)),ios::beg );


//reading the product record
Prodotto prodotto;
readFile.read(reinterpret_cast< char*> (&prodotto),sizeof(Prodotto));


readFile.close();
if(prodotto.getCod()==0)
{



//THIS PART IS ONLY ABOUT VARIABLES TO SET--> SKIP

cout<<"Inserisci il nome del prodotto (max 30 caratteri): ";
cin.ignore();
getline(cin,name);

if(name=="0") return;//condizione uscita

cout<<"Inserisci quantità da aggiungere: ";
cin>>quantita;

if(quantita==0) return;

cout<<"Inserisci il prezzo del prodotto: " ;
cin>>prezzo;

if(prezzo==0) return;

cout<<"Inserisci sezione di appartenenza del prodotto(max 15 caratteri): " ;
cin.ignore();
getline(cin,sezione);

if(sezione=="0") return;

cout<<"Aggiungi una descrizione del prodotto (max 500 caratteri):";
cin.ignore();
getline(cin,descrizione);

if(descrizione=="0") return;

//END OF THE VARIABLES INSERTION


//NOW PUT THE VARIABLES IN THE PRODUCT prodotto

    prodotto.setCod(codice);
    prodotto.setNome(nome);
    prodotto.setQta(quantita);
    prodotto.setPrz(prezzo);
    prodotto.setSez(sezione);
    prodotto.setDes(descrizione);

//open the file as output
ofstream inOutProd("prod.dat", ios::out );
if(!inOutProd){
        cerr<<"Error in the file creation";
 exit(1);
    }

//pointer for the location of the new record
inOutProd.seekp(var * (sizeof(Prodotto)));


//write the record prodotto
inOutProd.write(reinterpret_cast<const char*>(&prodotto),sizeof(Prodotto));

//i believe the problem is in the statement over this line. I tried also to add ios::app
// in the ofstream line but it only append the new record at the end of file.



cout<<"the following product has been saved successfully\n"<<endl;
outputLine(cout,prodotto);
inOutProd.close();

}
else
{
    cerr<<"The product"<<codice<<"is already in our database"<<endl;

}

}

if u can help me i will hug u untill the next Christmas

line 95 needs to have std::ate in the second parameter to tell fstream not to erase existing file contents. Note for ofstream ios::out is unnecessary because that's the default. See flags here

ofstream inOutProd("prod.dat", ios::ate );

yeah i think that is the problem, but ios::ate doesn't change anything unfortunately

Then try one of the other flags. Keep trying until you get the results you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.