Like Tree1Likes
  • 1 Post By Salem

files fstream function iin c++

This is a discussion on files fstream function iin c++ within the C++ Programming forums, part of the General Programming Boards category; i have the following code and im trying the following: i need to clear the file data and insert data ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    files fstream function iin c++

    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();
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    A development process
    Your main() is 150+ lines long already, and no end in sight as to what it might be when you've finished.

    Create specific functions to readFile and writeFile, so the logic of each is obvious and contained.

    You also open the same file 3 times without ever closing it - this is not good.
    laserlight likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specific data extraction from .txt files using fstream.h
    By xScen3xHdstyl3x in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2011, 01:58 AM
  2. fstream binary files
    By wesdgreat in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 09:12 PM
  3. Reading Files with fstream
    By pinkcheese in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2002, 10:10 PM
  4. Can I not call fstream to write to two files sequentially?
    By tigeress in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2002, 12:26 PM
  5. deleting files not in fstream?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2001, 01:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21