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. Advertisements

  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. Advertisements

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. Shalini
    Replies:
    2
    Views:
    673
    Brian Genisio
    Jan 9, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    1,103
    Keith Thompson
    Mar 6, 2005
  3. Replies:
    3
    Views:
    1,062
    Frederick Gotham
    Nov 30, 2006
  4. Gary Wessle

    using set.count() return

    Gary Wessle, Mar 1, 2007, in forum: C++
    Replies:
    1
    Views:
    394
    Marcus Kwok
    Mar 1, 2007
  5. , India
    Replies:
    2
    Views:
    1,138
    James Kanze
    Feb 14, 2008
  6. , India
    Replies:
    7
    Views:
    456
    kwikius
    Apr 28, 2008
  7. efelnavarro09
    Replies:
    2
    Views:
    1,177
    efelnavarro09
    Jan 26, 2011
  8. Jean-Sébastien

    count : array to array of hashes

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