I have another online "fill in the ..." programming example i'm having trouble with. It has to do with reading binary files and this is the first problem they give:
Open the file with the given name as a binary file. Count how often each byte value (between 0 and 255) occurs in the given file. A byte value returned by infile.get outside the range from 0 to 255 indicates the end of the file. Also compute the length of the file.
The program will then print all byte values that occur at least 2 percent of the time. (Horstmann, 2017-08-12, p. 287)
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
int byte_counts[256];
for (int i = 0; i < 256; i++)
{
byte_counts[i] = 0;
}
fstream infile;
string filename = "queen-mary.bmp";
int length = 0;
// full here
while (input > 0 && input < 256)
{
int input = infile.get();
length++;
}
for (int i = 0; i < 256; i++)
{
if (byte_counts[i] >= 0.02 * length)
{
cout << i << ": " << byte_counts[i] << endl;
}
}
return 0;
}
I have no idea how to go about this or even sure what they are asking, i'm not sure why they have an array set up either. I believe they are asking for me to count how many types a byte value shows up in a file?
Look closely at your instructions, this part in particular: "A byte value returned by infile.get outside the range from 0 to 255 indicates the end of the file"
You're only checking for values greater than 255, but you forgot about values less than zero. Note: you would probably be better off testing the state of the stream instead of looking at the actual value.
Where are you "Also compute the length of the file." and "The program will then print all byte values that occur at least 2 percent of the time."?
A byte value returned by infile.get outside the range from 0 to 255 indicates the end of the file.
this makes no sense. either its a signed byte, in which case, its -127 to 128 or whatever, or its unsigned, and its 0-255. There are no 8 bit bytes outside of 0-255. Either infile.get returns something other than a byte, or its no 8 bit system, or someone is on drugs.
I would forget all this, and just open the file, read all the bytes (you can seek the end to get the file's size in bytes if you want an old school way) and count them. To do that, you can make an array of unsigned int size 256 and increment each one as you find it, treating the bytes of the file as array index, eg if the first thing in the file is 0xFF then countarray[255]++ and so on.
You need to read char's not int's otherwise you'll go nowhere.
It looks also like the end of file comment in the question is wrong because you will be very lucky to go past the first line.
The program is a little bit like counting characters - use the array for that - if you have an input char such that its value (ASCII) is 253, then byte_count[253]++ etc etc That goes inside the while loop
I reckon the 0.2 refers to printing out only 20% of the input chars as the while loop proceeds.
At the end outside the while loop display length then the array totals.
@OP
Take your pick. Best choose a smaller picture of the Queen Mary than I did:
@OP take your pick.
I suppose I could have picked a smaller picture of the Queen Mary.
@OP: your two code snips are too similar, I see that you open the file and «fixed» the compile error
¿why did you post the first one?
> what they are asking (...)
> I believe they are asking for me to count how many types a byte value shows
> up in a file?
the problem statement says «Count how often each byte value (between 0 and 255) occurs in the given file»
¿what part you don't understand?
¿can you do «count how many 'a' are in this sentence»? then count each vowel, then instead of a sentence a whole file, then count each letter
it's all the same idea
> Where are you "Also compute the length of the file." and "The program will
> then print all byte values that occur at least 2 percent of the time."?
@jlb: ¿are you testing the OP? those are the only things that are done because (I guess) they were on the original question
sure, it's not counting, but the print part it's done.
> You need to read char's not int's otherwise you'll go nowhere.
¿ever heard of overflow?
> while (!infile.eof()) // <-- eof() SORT OF BAD BUT WORKS FOR DEMO
indeed, shows the one off error which is the reason you shouldn't check for eof that way
@Handy Andy: nice, let's just solve the OP homework, that'll teach them.
@againtry's second code: it's like you are seeing all the blood but don't realise that you just blow off your foot.