473,766 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I return the count of an array?


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(unsigne d);

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

ifstream in;
in.open(filenam e, 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(unsigne d u)
{
register int b;

for (b = 128; b 0; b = b/2) {
(u & b) ? (cout << '1') : (cout << '0');
}
}
Thanks in advance for your help.

Sep 11 '08 #1
2 1700
Sam
Mesvak writes:
>
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...
You probably mean bytes, not bits. No modern architecture could possibly
hold a file that contains exactly 10529 bits.
I want it to print that value also. Any
suggestions?
Since this is C++, use std::vector.
Code:
void dBinary(unsigne d);
std::vector<uns ignedmyArray;

Your homework assignment is to write the code for dBinary that adds a new
element to myArray. Then, the number of elements read is simply:

myArray.size();

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkj JrKYACgkQx9p3GY HlUOJXLACdF2Z0W He+DE2ipCeuYl22 OUQo
6fQAn3pLQM34fY2 t6r9RMqh3rpX7C4 nA
=s4TF
-----END PGP SIGNATURE-----

Sep 11 '08 #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(filenam e, 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);
Sep 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5767
by: Shaine Fisher | last post by:
I have had a look around but I'm either not finding or not seeing the answer to this, what i want to do is reurn the results as an array, or some other worldly useful format that flash mx2004 can read. But when I did thiss I was told datasets were the way to go, but needs an array, prefer typed as were changing it. Here is the current code: (shortened for readability) public class WebService1{
2
5788
by: Jayme Pechan | last post by:
Is there a simple way to return a COM enumeration interface from .NET? I am porting a C++ COM Object that has an enumeration interface containing the DISPID_NEWENUM and DISPID_VALUE properties and the count property and need to create the equivilant in C#. I'm hoping I can just return an array list or something but something tells me it isn't that easy. Any thoughts on the proper way to do this in C#? Thanks. Jayme
8
2230
by: solomon_13000 | last post by:
The code bellow functions well. However if I want to obtain a return value using the code bellow, how is it done? <% Sub RunQueryString (pSQL,parms) on error resume next Set conn = Server.CreateObject("ADODB.Connection") conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/db/upload/stelladb.mdb") & ";" Set cmd = Server.CreateObject("adodb.command")
19
1946
by: Andrew Gentile | last post by:
Hello, I have been working on a program where I need to have a function return an array. I found out that C doesn't do this, so now I am trying to get the function to return a pointer to an array. It seems easy, but I keep running into the same problem. The function appears to execute properly, but when I print out the array values, only the first one is correct. The other are all wrong. A snippet of code is below. Please take a...
10
5616
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
10
5906
by: vignesh4u | last post by:
I am trying to implement the Split function in C ie. if i have a string: char* S="This is a test"; and if i try to split based on space ' ' it should return an array of strings like: {"This","is","a","test"} . I tried to implement it as given below but am getting a segmentation fault. I would really appreciate if some one could give me an answer on this issue:
53
3419
by: yinglcs | last post by:
Hi, In java, there is an 'indexOf' function in String which does this: indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. is there anything like that in c? The closest thing I can find is strchr, but it check for a character,
12
2314
by: subramanian100in | last post by:
Below is my understanding about count algorithms. Return type of count and count_if algorithms is iterator_traits<InputIterator>::difference_type. If the container contains more than 'difference_type' elements satisfying the condition, then count and count_if algorithm cannot return a value greater than 'difference_type'. As an example, suppose maximum value of 'difference_type' is INT_MAX.
0
1420
by: constructor | last post by:
Hello NG, I'm having to use a function in an external library that returns a pointer to an array of structs. The function declaration according to library vendor is: device** getList( int &count) The returned structs: typedef struct tag_dev {
1
3203
by: claudfs | last post by:
Hey Guys, Ok here is my problem: There is around 350 rows in the db. All the variables $actor_id, $comment_id and $likes_id are all uid's for facebook users Now i want to group and sum how many entries there are of each unique uid
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.