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 gkittelson 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;
}
 
Code:
char byte[10];
If you want 10 characters, you will need to set this up as
Code:
char byte[11];
. In C and C++, character arrays are null terminated. This means that you must include one extra place for this termination character. Start with this and see what happens after that. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top