Can I return the count of an array?

M

Mesvak

Hi,

I have this code which takes in a file, reads it in as a binary array,
and prints the array.

Is there anyway I can write a code which would print the number of
binary values taken and printed? like a counter? So basically if a
file consists of 10529 bits... I want it to print that value also. Any
suggestions?


Code:
void dBinary(unsigned);

int main()
{


char filename[60];
cout << "Please enter the filename you wish to use to embed: \n";
cin >> filename;

ifstream in;
in.open(filename, ios::in);
char ch;

// Assign each byte to ch
// and then pass it to the function dBinary
while (in.get(ch)) {
dBinary(ch);
}

in.close();
cout << endl << endl << endl;
return 0;
}

void dBinary(unsigned u)
{
register int b;

for (b = 128; b > 0; b = b/2) {
(u & b) ? (cout << '1') : (cout << '0');
}
}


Thanks in advance for your help.
 
J

Juha Nieminen

Mesvak said:
char filename[60];
cout << "Please enter the filename you wish to use to embed: \n";
cin >> filename;

What happens if the user writes more than 59 characters? ("But I am
the only one who is going to use this." Fine, but you can make a mistake
too.)

What's wrong with using std::string?
ifstream in;
in.open(filename, ios::in);

Nitpicking, but given that ios::in is the default for std::ifstream,
you don't have to specify it. Besides, you can open the name in the
constructor:

std::ifstream in(filename);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top