How to put stack infile stream

This is a discussion on How to put stack infile stream within the C++ Programming forums, part of the General Programming Boards category; Hi guys I am presently using stack for storing variables using push and pop operation in C++,since my requirement of ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    9

    How to put stack infile stream

    Hi guys
    I am presently using stack for storing variables using push and pop operation in C++,since my requirement of these variables is in reverse order .However i want to put these variables in file I/O using fstream as i want to use pop operation in different program. can u suggest me which comd to use as well as any other efficient means to solve this problem. Data type I am using is uint32_t and these variables will be called by various other modules of the prog and need to be used only once thus stack is fulfilling my purpopse.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Generally you use "<<" to put things into a stream.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    9

    How to put stack infile stream

    Thanks,
    The piece of code for stack function is as under
    //PUSH Function
    Code:
    template <class Stacktype> void Stack<Stacktype>::Push(Stacktype ob)
    	{
    		if(tos== size) {
    		cout<<"Stack is full "<<endl;
    		return;
    		}
    		else{
    		ofstream outsession("stk_chain", ios::out);
    		if(!outsession){
    			cout<< "Cannot open file"<< endl;
    			return ;
    			}
    		stck[tos] = ob;
    		outsession.write((const char *)&stck[tos], sizeof(Stacktype));
    		tos++;
    		outsession.close();
    		}
    	}
    	
    int main(){
    	Stack<uint32_t> s1;
    	uint32_t val;			
    		if(!s1.Push(val))
    					cout<<endl << "Stack Full";
    		else{
    			cout<<endl<<"Push successfull";
    			printf("%.8x\n", val);
    					
    		}
    	}

    above fn is working fine with other data type like char int etc .My prog is generating uint32_t value,for eg( 34cbbc6e,cdd896b0,c7fb864d,74cd8776).It gives following error on compilation
    " stkfile_clhash.cpp:70: error: could not convert ‘s1.Stack<Stacktype>::Push [with Stacktype = unsigned int](val)’ to ‘bool’
    stkfile_clhash.cpp:70: error: in argument to unary ! "

    pl help

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want to use the return value of a function, you should make it return something. You've got a pretty obvious use case here for a boolean return (true on success, false on failure), so make it so.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    9

    Smile How to put stack infile stream

    Thanks a lot to everybody for good cooperation.
    problem resolved
    rgds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 11:18 AM
  2. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  3. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  4. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 10:13 AM
  5. Array Stack Problem
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2001, 06:58 PM

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