Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BITS & BYTES???

Status
Not open for further replies.

benlogan1981

Programmer
Dec 9, 2001
14
GB
OK, I'm having a few difficulties here. New to C++!
I have an input file of 0s and 1s, which are meant to represent bits. I want to read 10 of these characters in and create a 10 bit byte with them. Then I store this byte into an array to represent memory[256]. And then read another 10 'bits' from the file, etc
This is what I have, but when I try to progress from here I just mess it up...

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>

char *infile = &quot;data.txt&quot;;

int main()
{
ifstream iFile(infile, ios::in);
char memory[256];
char byte[10];
//read operation - for cycling through the bytes
//for(int i = 0; i < 16; i++) { //loop 16 bytes
//iFile >> memory; //from file, shouldnt be !!!
//}
//read operation - leave when input buffer is full

//read operation - bits for creating the bytes
for(int d = 0; d < 10; d++) {//loop 10 bits in bytes
iFile >> byte[d]; //from file
}
//read operation - leave when input buffer is full

for(int e = 0; e < 10; e++) {
cout << byte[e];
}//displaying a byte

//transfer contents of full input to the output
int x = 0;
for(int j = 16; j < 32; j++) {
memory[j] = memory[x];
memory[x] = 0;//clearing the input buffer
x++;
}

//write operation - to screen or file
for(int a = 0; a < 16; a++){
cout << &quot;Input Buffer: &quot; << memory[a];
cout << &quot; Held at LOC: &quot; << a << endl;
}
for(int b = 16; b < 32; b++){
cout << &quot;Output Buffer: &quot; << memory;
cout << &quot; Held at LOC: &quot; << b << endl;
}
//write operation
return 0;
}
 
Please give us some more information,
Do you want the first 'byte' to be at the end of your 'memory' when you're done?
Are you new to programming to ? You're not planning to write a whole lot of them loops to shift your 'bits' do you ? You should work with counters to keep position in your
'memory',if I understand it correctely.
is this what you want ?:

file->10 chars -> memory array (at end)
file->10 chars -> memory array (end-10)
file->10 chars -> memory array (end-20)
...



The Muppeteer.



Don't eat yellow snow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top