i have the following code and im trying the following: i need to clear the file data and insert data in the cleared file on the other hand i need to read the file information the following is the code i m using and im facing problems as the file is not cleared and i dont n=know what i should do any help is really appreciated thank in advnace
Code:
#include<iostream>#include <fstream>
using namespace std;
#include "Cboard.h"
#include "Cpawn.h"
#include "Cpiece.h"
#include "Crook.h"
#include "Cknight.h"
#include "Cbishop.h"
#include "Cking.h"
#include "Cqueen.h"
void main(){
fstream outfile ("chess1.txt",ios::ate);
char location[50];
int confirm=0;
bool ismovepossible;
char tocheck;
int ischeck=0;
int oldrow=0,oldcolumn=0,newrow=0,newcolumn=0,check =0,end=0;
int exit=0;
char chose;
Cboard myboard;
Cpawn mypawn;
Crook myrook;
Cknight myknight;
Cbishop mybishop;
Cking myking;
Cqueen myqueen;
Cpiece *prook = &myrook;
Cpiece *ppawn = &mypawn;
Cpiece *pknight= &myknight;
Cpiece *pbishop = &mybishop;
Cpiece *pking = &myking;
Cpiece *pqueen=&myqueen;
outfile.open("chess1.txt");
std::cout<<"TO SEE PREVIOS MOVES PRESS Y to PLAY PRESS P ";
std::cin>>chose;
if(chose=='Y'||chose=='y'){
outfile.open("chess1.txt",ios::beg&&ios::out);//read from beginning
while(!outfile.eof()){
outfile>>oldcolumn;
outfile>>oldrow;
outfile>>newcolumn;
outfile>>newrow;
(myboard).moveer(&newrow,&newcolumn,&oldrow,&oldcolumn);
(myboard).printboard();
std::cout<<std::endl;
std::cout<<"press any key";
std::cout<<std::endl;
std::cin>>tocheck;
}
std::cout<<"all moves done";
}
//when entered in here i need to remove the content
if(chose=='P'||chose=='p'){
outfile.clear(); // clear file
do{
std::cout<<std::endl;
(myboard).printboard();
std::cout<<std::endl;
std::cout<<"please enter location";
std::cin>>location;
check =(myboard).validateuserinput(location,&newcolumn,&newrow,&oldrow,&oldcolumn);
if(check==1){
ismovepossible =false;
}
if(check == 0){
if(ischeck==1){
if((myboard).board[oldrow][oldcolumn]->getpiece()=='k'||(myboard).board[oldrow][oldcolumn]->getpiece()=='K'){
check=0;
if(myboard.isincheckmate(&oldrow,&oldcolumn,&newrow,&newcolumn,&myboard)==true){
check =0;
ischeck=0;
end=0;
ismovepossible =true;
}
else{
std::cout<<"Checkmate";
std::cout<<std::endl;
end=1;
}
if(myboard.kingcheck(&oldrow,&oldcolumn,&newrow,&newcolumn,&myboard)==true){
std::cout<<"Please move to another position";
std::cout<<std::endl;
ischeck=1;
ismovepossible=false;
check=1;
}
}
else{
check =1;
ischeck=1;
ismovepossible =false;
}
}
}
if(check==0&&end==0){
if((myboard).board[oldrow][oldcolumn]->movepiece(&newcolumn, &newrow,&oldrow,&oldcolumn,&myboard)==1){
ismovepossible =true;
}
else{
ismovepossible =false;
}
}
if(ismovepossible ==true){
if (outfile.is_open())
{
ios::app;//append the inputted data to the file
outfile<<oldcolumn<<std::endl;
outfile<<oldrow<<std::endl;
outfile<<newcolumn<<std::endl;
outfile<<newrow<<std::endl;
}
else cout << "Unable to open file";
(myboard).moveer(&newrow,&newcolumn,&oldrow,&oldcolumn);
(myboard).move();
if(myboard.incheck(&newrow,&newcolumn,&myboard)==true){
std::cout<<"King is in check ";
ischeck=1;
}
}
outfile.flush();
}while(end!=1);
}
outfile.close();
}