2 Replies - 456 Views - Last Post: 20 September 2012 - 07:35 PM Rate Topic: -----

#1 synarchtan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-September 12

Binary array to file problem

Posted 20 September 2012 - 07:07 PM

I am writing a C++ code and when I go to send the binary array from LSB to MSB it only sends the end value, what I want to do is to send the whole value: ex( binaryr[0] to binaryr[end#]) I would apprecate any clarification of what I am doing wrong. Here is my code, I will also include my txt file:


#include <iostream>
[#include <fstream>
using namespace std; 
#include <cstring> 
#include <cstdlib> 
 
char *entry, letter, choice[2]; 
int ascii, len, binary[8], binaryr[8], total; 
void prog(); 
 
int main() { 
      prog();      
      return 0; 
}         
 
void prog() { 
	entry = new char[101]; 
 
	cout<<"Enter string to convert (up to 100 chars): "; 
    cin.getline(entry, 100); 
	len = strlen(entry); 

//Converts the chars to bits('0' and '1' characters)
	for(int i = 0; i<len; i++) { 
		total = 0; 
		letter = entry[i]; 
		ascii = letter;
		
		while(ascii>0) { 
			if((ascii%2)==0) {
				binary[total] = 0; 
				ascii = ascii/2; 
				total++;
			} 
			
			else { 
				binary[total] = 1; 
				ascii = ascii/2; 
				total++; 
			}
		} 
		while(total>=0) {
			total--; 
		}

		//Method to order bits from least significant bit(LSB) to most significant bit(MSB).
		int k = 0;
		for (int i=7; i>=0; i--){
			binaryr[i] = binary[k];
			k++;
			cout<<binaryr[i];
		}
	}

	//Sends the bits to the transmitted_message file.
	cout<<endl<<"Write to file?(1 = yes and exits, 2= just exits)?: "; 
	cin.getline(choice,3);
	if(choice[0] == '1'){
                //Here is where I am having difficulty. I want it
                //to print out every binary value similar to what
                //is printed as a result.
		for (int i=0; i<=len; i++){
			ofstream file;
			file.open("transmitted_message.txt");
                        file << binary[i];
			file.close();
			exit(0);
		}
	}
	else 
		exit(0);
}


Here is what I get:
Enter string to convert (up to 100 chars): A
10000010
Write to file?(1 = yes and exits, 2= just exits)?: 1

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Binary array to file problem

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1915
  • View blog
  • Posts: 5,719
  • Joined: 05-May 12

Re: Binary array to file problem

Posted 20 September 2012 - 07:27 PM

Look closely at your lines 62-68. There are two major issues there:
- exit() call from within the loop
- reopening the file from within the loop

This post has been edited by Skydiver: 20 September 2012 - 07:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1915
  • View blog
  • Posts: 5,719
  • Joined: 05-May 12

Re: Binary array to file problem

Posted 20 September 2012 - 07:35 PM

In general, you should never call exit() from within a C++ program because it doesn't unwind the stack. Any objects which should have their destructors called won't have an opportunity to be called.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1