Thread: Saving a binary file

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

    Saving a binary file

    I tried to put some decimal numbers and save it as a binary numbers in the hard drive .rft file.I wrote the code but somehow its not works. Any help for that?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int binary(int);
    
    int main(void) {
    
    int number;
    
    ofstream MosFile("Milloin_two.rtf");
    
    
    if(MosFile.is_open()) {
    cout<<"ok the file is open"<<endl;
    }
    
    else{
    cout<<"Its messed up"<<endl;
    }
    
    cout<<"Enter a positive integer:";
    cin>>number;
    
    for(int i=1;i<=number;i++)
    
    {
    
    MosFile<<MosFile.binary(i)<<"\n"; // I wanted to use binary function to convert decimal to binary number. MosFile for using binary function.
    
    }
    
    MosFile.close();
    }
    
    
    
    int binary(int number) {
    
        ofstream MosFile;
    
    	int rem;
    
    	if(number <= 1) {
    		cout << number;
    		return;
    	}
    
        rem= number%2;
    	binary(number>> 1);
    	MosFile<<rem;
    }


    Also, what will be the return type of the binary function ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post the complete error messages, exactly as they appear in your development environment.

    Jim

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    73
    I changed it a little and now :

    Code:
    #include <iostream>
    
    using namespace std;
    
    int binary(int);
    
    int main(void) {
    
    int number;
    
    ofstream MosFile("Milloin_two.rtf");
    
    
    if(MosFile.is_open()) {
    cout<<"ok the file is open"<<endl;
    }
    
    else{
    cout<<"Its messed up"<<endl;
    }
    
    cout<<"Enter a positive integer:";
    cin>>number;
    
    for(int i=1;i<=number;i++)
    
    {
    
    for( int i  = 0 ; i <  number  ; i++ )
    {
      	MosFile<<binary( number )<<"\n" ;
    }
    
    }
    
    MosFile.close();
    }
    
    
    
    int binary(int number)
    {
    	if( number <= 1 )
    	{
    		cout << "Number ="<<number ;
    		return 0 ;
    	}else
    		{
    			int rem = number %2 ;
    			binary ( number >> 1 ) ;
    			return rem;
    
    		}
    }

    The error is :

    D:\Lehrstuhl fur Aerodynamik und Stromungsmechanik\bluecoder.cpp||In function 'int main()':|
    D:\Lehrstuhl fur Aerodynamik und Stromungsmechanik\bluecoder.cpp|11|error: variable 'std:fstream MosFile' has initializer but incomplete type|
    ||=== Build finished: 1 errors, 0 warnings ===|

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you included the proper include file for fstream?

    Jim

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent properly, include <fstream>, remove MosFile.close().
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    73
    I included the proper file. Thanks Now, its running but not doing the same what I needed to do. I want to put some decimal numbers using for loop. Say, I put 13. Then, it will take 1,2,3,4,5,...,13 & converts them to binary numbers that will be saved in a file in hard drive. The output is now:
    Code:
    ok the file is open
    Enter a positive integer:5
    Number =1Number =1Number =1Number =1Number =1Number =1Number =1Number =1Number =
    1Number =1Number =1Number =1Number =1Number =1Number =1Number =1Number =1Number
    =1Number =1Number =1Number =1Number =1Number =1Number =1Number =1
    Process returned 0 (0x0)   execution time : 9.078 s
    Press any key to continue.
    That was in cmd. The file Milloin_two.rtf was also created but theres not expected output. Any suggestion from you ?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I take it that if you take 11, you want it to write 1011 to the file, right?
    If so, you need to redesign your binary function. Once you understand how it works, you should also understand why it fails. Right now, it's written so that it will output the individual bits to a stream and not return the binary representation of a file.

    Anyhow, I recommend you write your own number -> binary function such that you fully understand it. Then make it work in such a way that it can output the binary representation to file.
    Last edited by Elysia; 01-29-2012 at 12:46 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually this doesn't even print all of the bits, it just returns them. And on top of that the OP is overwriting his work. There can only be one return value, so if you want to save your work, the final result must be cumulative; for example instead of returning a number, you might append the bit to a string.

    The lowest bit is the only bit that is ever printed. Take note that your function in the other thread printed bits even when the if (number <= 1) was false.

    Code:
    #include <starship>
    
    string binary(unsigned int x, string &retval)
    {
       char bit = '0' + x % 2;
       if ( x <= 1 ) {
          return retval + bit;
       }
       return binary(x / 2, retval) + bit;
    }
    Something like that.
    Last edited by whiteflags; 01-29-2012 at 01:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

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