Can I return the count of an array?

Discussion in 'C++' started by Mesvak, Sep 12, 2008.

  1. Mesvak

    Mesvak Guest

    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.
     
    Mesvak, Sep 12, 2008
    #1
    1. Advertising

  2. 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);
     
    Juha Nieminen, Sep 12, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greenhorn
    Replies:
    15
    Views:
    852
    Keith Thompson
    Mar 6, 2005
  2. Gary Wessle

    using set.count() return

    Gary Wessle, Mar 1, 2007, in forum: C++
    Replies:
    1
    Views:
    347
    Marcus Kwok
    Mar 1, 2007
  3. , India
    Replies:
    2
    Views:
    1,081
    James Kanze
    Feb 14, 2008
  4. efelnavarro09
    Replies:
    2
    Views:
    958
    efelnavarro09
    Jan 26, 2011
  5. Jean-Sébastien

    count : array to array of hashes

    Jean-Sébastien, Aug 1, 2007, in forum: Ruby
    Replies:
    2
    Views:
    133
    Stefano Crocco
    Aug 1, 2007
Loading...

Share This Page