Thread: Saving as a Binary file

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

    Saving as a Binary file

    I posted it before. I got some answers but wont be able to run the program. The idea is to put some Decimal numbers and get it as binary in a file. The program is-


    Code:
    
    #include <iostream>
    #include<fstream>
    
    using namespace std;
    
    int Binary( int, int );
    
    int main()
    {
        ofstream MosFile("Milloin_Binary.rtf");
    
    
    	int decimal;
    	int a,o;
    	int b[1000000];
    
        cout << "Enter a Decimal Integer to convert it to binary: ";
    	cin >> decimal;
    
    	for(a=1,o=0;a<= decimal;a++,o++)
    
    	{
    
        b[o]=Binary( a,2 );
    
        MosFile<<b[o]<<"\n";
    
    
    	}
    
    MosFile.close();
    
    }
    
     int Binary(int num, int base)
    
    
    {
        if (num > 0)
    
    
    	{
    		Binary(num/base, base);
    		return (num % base);
    
    	}
    }
    Any advice to modify it for accomplishing the goal ?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Didn't I show you how to do this with a string yesterday?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Your Binary() function is no good. The recursion does not accomplish anything and will continue infinitely.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by MK27 View Post
    Your Binary() function is no good. The recursion does not accomplish anything and will continue infinitely.
    That's not going to be inifinitely recursive unless base is 1. num is divided by base upon each call with the recursion terminating when num is zero.

    It does lack any means of actually returning a sensible value to the caller though. The original poster needs to turn their warning levels up and fix the warnings.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving a binary file
    By chaklader in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2012, 01:09 PM
  2. Saving Obj to Binary File
    By j_d in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2011, 12:57 PM
  3. Saving map to binary file.
    By Qrypt in forum Game Programming
    Replies: 2
    Last Post: 04-14-2011, 03:03 AM
  4. Saving Struct to Binary File/Copying Structs
    By timmeh in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2009, 08:44 PM
  5. Saving linked lists to a binary file
    By kotoko in forum C Programming
    Replies: 1
    Last Post: 06-14-2008, 06:41 PM