binary file

This is a discussion on binary file within the C++ Programming forums, part of the General Programming Boards category; can someone pls tell me what is wrong with my program Code: #include <iostream> #include <fstream> using namespace std; int ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    binary file

    can someone pls tell me what is wrong with my program

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
    	fstream rfile;
    	rfile.open("ITEMS.DAT", ios::in|ios::out);
    	if(!rfile)
    	{
    		cout<<"error\n.";
    		exit(1);
    	}
    	int c=0;
    	cout<<"Enter c\n";
    	cin>>c;
    	cin.get();
    	rfile.seekp(0, ios::end);
    	rfile.write( (char*) (&c), sizeof(int) )<<flush;
    	rfile.seekg(0, ios::beg);
    	while (!rfile.eof())
    	{
    		rfile.read( (char*) (&c), sizeof(int));
    		cout<<"c is:"<<c<<endl;
    	}
    	rfile.clear();
    	rfile.close();
    	return 0;
    }
    // the output is this

    //enter c

    // c is:2
    // c is:2

    question: why are there two outputs of 2 even though the file is clean and i only enter one 2?

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    Smile thank u salem

    thank u salem you have been a great help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-24-2006, 11:43 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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