Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I return the count of an array?

Reply
Thread Tools

Can I return the count of an array?

 
 
Mesvak
Guest
Posts: n/a
 
      09-11-2008

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.

 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      09-12-2008
Mesvak wrote:
> 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);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count = Count + 1 Using only std_logic_1164 Doubt efelnavarro09 VHDL 2 01-26-2011 03:49 AM
using set.count() return Gary Wessle C++ 1 03-01-2007 07:33 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from? Kaimuri MCSD 3 12-29-2004 06:38 PM
I am adding a new row to the datagrid dynamically but if i use the Count property of Item it is not showing the count of the new rows being added Praveen Balanagendra via .NET 247 ASP .Net 2 06-06-2004 07:16 AM



Advertisments