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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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 ?
I think that line 29 is not ok...
Thanks. Do you know that how to fix that ?
Jumper is correct. std::ofstream doesn't have a method (a function that's associated with a class/struct) called binary(). Instead, it's a global function that needs to be invoked without being qualified with a class name.

Wazzak
This could be .. what you are looking for . .

//This is for loop in the main .
1
2
3
4
for( int i  = 0 ; i <  number  ; i++ ) 
{
  	MosFile<<binary( number )<<"\n" ; 
}


//this is the binary function .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int binary(int number) 
{
	if( number <= 1 ) 
	{
		cout << "Number ="<number ;
		return 0 ; 
	}else
		{
			int rem = number %2 ; 
			binary ( number >> 1 ) ;      
			return rem; 
			
		}
}


I am not able to understand why you are using this recursive function binary ( number > > 1 ) over here , what will be the funtionality . of this or what does it exactly do
Last edited on
+1 rep Wazzak
+1 rep bluecoder
So I changed that and still its showing one error-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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;

		}
}




Any other advice for that ?
Last edited on
I know that its possible to do without recursion but still want to try this way :)
You just make it worse for you. Anyway could you tell us what the compiler says ? (what error does it give ? )
int main(void) still doesn't make sense to me ... I'm a bit confused. You want the main to return void ?



http://www.cplusplus.com/forum/general/60414/

Decimal to binary; Binary to file; What's next ??
Last edited on
When void is used in this context, it indicates explicitly that the function doesn't require any arguments to be passed to it prior to its invocation. In C++, a set of empty parentheses mean exactly the same thing.

First, you check to see if the opening of the file fails. However, if the opening fails, you don't handle it appropriately. Regardless, you continue to write to a file that may be non-existent.

Also, in function binary(), I would parenthesise this expression: number %2. Finally, why do you invoke binary() and ignore the value it yields?

Wazzak
Last edited on
Thanks for the explanation.
I forgot the include something in the top. The code is now-


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>


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 file is opened. 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 in cmd is now:

1
2
3
4
5
6
7
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.



The file Milloin_two.rtf was also created but theres not expected output. Any suggestion from you ?

It is easy to see that your program stucks here:

1
2
3
4
5
if( number <= 1 )
	{
		cout << "Number ="<<number ;
		return 0 ;
        }


But the problem is here:

1
2
3
4
5
6
7
8
9
10
for(int i=1;i<=number;i++)

{

for( int i  = 0 ; i <  number  ; i++ )
{
  	MosFile<<binary( number )<<"\n" ;
}

}


You cannot declare int i two times in a loop!! Your code should look like this:

1
2
3
4
5
6
7
8
9
10
for(int i=1;i<=number;i++)

{

           for( int j  = 0 ; j <  number  ; j++ )
           {
              	MosFile<<binary( number )<<"\n" ;
           }

}


Try this, and let me know if it works :D
Last edited on
I wanted to ask you, what do you use to get the timing ? execution time : 9.078 s ?
jumper007 wrote:
You cannot declare int i two times in a loop!! (sic)

Actually, you can. The compiler will assume the innermost i variable.

@arefe. You're still doing it. You're not handling a file opening failure correctly. It should resemble something like this:

1
2
3
4
5
6
7
if(!MosFile.is_open())
{
    // The file failed to open. We cannot write to the file.
    std::cout << "Fatal: The file failed to open." << std::endl;

    return(1);
}

Also, the innermost loop is pointless. You need to remove it. If you don't, in total, your loops will loop 25 times.

Wazzak
Last edited on
O.o My teacher always told me to stop using the same variable two times in loop (int i). She said that the compiler will return errors. Can you explain that in detail please ? Or give me a link ?

And, I also have a thing that I'm not sure about. I know the difference between ++a, and a++.
But what would be the difference if instead of i++ i would write ++i (in a for loop) ?

1
2
3
4
for (int i=0; i<=n; ++i)
{
//for loop will start from i=1 ? or from i=0 ?
}
Last edited on
Here's how it works: For each scope created, a new stack frame is created on the program's stack. Each stack frame has it's own variables, function calls, etc. A for loop is no different. Now, here's our two loops:

1
2
for(int X(0); ...)
    for(int X(0); ...);

OK, now. Two stack frames have been created here: 1 for the innermost, and 1 for the outermost. Both stack frames have their own X variable pushed onto them. Since the innermost loop is the most recent stack frame, the compiler will look into it, and will see X there, and it'll use that. It won't search any further, because what's the point is searching for something that you've found? I guess you could rename the innermost X to disambiguate it.

jumper007 wrote:
But what would be the difference if instead of i++ i would write ++i (in a for loop) ? (sic)

There's no difference unless the expression has more than two operands/sub-expressions, like so:

for(int X(0); ... ; ((++X) + 1))

The above expression within the increment section is when the post-fix/pre-fix ++/-- matters.

Wazzak
Last edited on
Thanks for promptly respond. I finally cleared these two issues :) Thanks again.
Last edited on
Topic archived. No new replies allowed.