Reading integers store in 4-bits from a fstream in C++ (Homework confusion)
3 replies, posted
Hey, normally I don't ask for help for H/W but facepunch has be so responsive and fast in resolving small confusions in my assignments before which saves me a lot of time. I already finished Exercise A (which was far harder), but I'm having a hard time understanding exactly what fstream functions I should be using for Exercise B.
[quote]
Exercise B — “numberChase” function
In this exercise, you will write a function to read a binary file in "random access" mode. Your function will open a file that contains a list of 4-byte binary integers. You should read the first integer to find the index of the next integer, print this index to an ostream, and repeat until you hit a negative integer. *IF* the file was an array of integers, you'd be doing something like this:
int i=0;
while (i >=0 )
{
i=myArray[i];
cout << i << "\n";
}
You can implement this using either seekg to jump around in the file (remember that each integer is four bytes!), or by reading the whole file into a big array first. I promise I won't make an infinite loop or an out-of-bounds index--if your indices repeat or become huge, then you're seeking to the wrong spot!
Your function should be called numberChase and should take two parameters, a C++ string for the filename, and an ostream to print the indices too.
(HINT: The first file the testing program checks contains the four integers +3 0 -77 +2; you're supposed to follow the integers. So after reading the 3, you jump to *integer* 3; this has the value 2, so you jump to *integer* 2; this has the value -77 so you stop.)
[/quote]
The start of my code (already got it working with his tester program and declared the function in my header). Right now I'm trying to get it to read the integers properly and I'll just store them in an array instead of using seekg():
[code]
void numberChase(string fileName,ostream& out){
ifstream file;
file.open(fileName,ios::in|ios::out|ios::binary);
for(int i=0;i<4;i++){
cout<<file.get();
}
}
[/code]
Also, here's the jibberish in the data file:
[code] ³ÿÿÿ [/code]
How do i use seekg() and tellp() properly??? I googled stuff but it was too confusing/unrelated to the problem.
Thank you so much.
[editline]8th February 2011[/editline]
Never mind. I got it to work by looking at an example of writing an array to a file with write().
[code]
void numberChase(string fileName,ostream& out){
ifstream file;
int arr[4];
file.open(fileName,ios::in|ios::out|ios::binary);
file.read(reinterpret_cast<char *>(arr),16);
for(int i=0;i<4;i++){
cout<<arr[i]<<endl;
}
}
[/code]
[QUOTE=newbs;27947789]
Also, here's the jibberish in the data file:
[code] ³ÿÿÿ [/code]
[/QUOTE]
I find it scary that I know what the gibberish means in it's binary counterpart :ohdear:.
I'm not going to do your homework for you, it's up for you to work it out as you need to have the skills to find solutions to problems like that.
Anyway, besides that, you need to know the difference between a bit and a byte first.
[QUOTE=Jookia;27949848]I'm not going to do your homework for you, it's up for you to work it out as you need to have the skills to find solutions to problems like that.
Anyway, besides that, you need to know the difference between a bit and a byte first.[/QUOTE]
I do, I finished the program but I don't fully understand it.
Sorry, you need to Log In to post a reply to this thread.