|
-
May 13th, 2008, 03:31 PM
#1
cin NUL
Hi,
I apologize if this is the wrong forum for this question. I need to read in the ascii NUL char (all zeroes). However, cin and getChar both ignore this character. The reason I need to read NUL is because I am implementing a Huffman Code decoder and (encoder) and it is possible that the NUL character is, or is part of, a valid Huffman Code.
Thanks,
Steve
-
May 13th, 2008, 03:47 PM
#2
-
May 13th, 2008, 05:49 PM
#3
Re: cin NUL
getline isn't that good of a solution either because it either delimits on '\n' or on a specified char. However, all chars in the ascii char set are valid input for my program. Is it possible to delimit on NULL (not to be confused with NUL), because then it might work. I don't have time to test right now, but I'll do it later.
-Steve
EDIT: I just tested getline and it doesn't seem to see the NUL char either. I guess it's possible that the encode function isn't properly writing the NUL char. But I've looked over the code and can't seem to see anything that could be causing problems. It works for all values other than NUL (00000000).
Code:
number=0;
for (i=7; i>=0 ; --i)
if ( buffer[i] == '1')
number += (int)pow(2,power++);
else power++;
cout << (char)number ;
Last edited by TheXenocide; May 13th, 2008 at 06:32 PM.
-
May 13th, 2008, 06:36 PM
#4
Re: cin NUL
cin.get() works for me. Can you give an example that fails? What do you mean by "works", are you expecting cout to output something?
Why are you using cin? Is the data coming from the console?
-
May 13th, 2008, 07:03 PM
#5
Re: cin NUL
 Originally Posted by jlou
cin.get() works for me. Can you give an example that fails? What do you mean by "works", are you expecting cout to output something?
Why are you using cin? Is the data coming from the console?
By "works" I mean that the function call actually retrieves the NUL char and stores it into the specified variable. Yes, when I call cout like this:
Code:
int number = 0;
cout << (char)number;
I expect that the NUL char is actually printed (but I don't expect to visibly see it).
I am using cin and cout because input has been redirected into the program and the output is being redirected to either a file or another program.
Here is the relevant decode code:
Code:
//The following code that is commented out is to show some of the different functions I have tried.
//while (count < length && (c = getchar())!= EOF )
// buffer1[count++] = c;
//cin >> noskipws;
//while(count < length && !cin.eof())
// cin >> buffer1[count++];
while(count < length && cin.good())
buffer1[count++] = cin.get();
//Special case for data ending in 1st buffer
if (count < length )
lastChar1[count-2] = true;
i = 0 ;
while (true)
{
//Buffer is almost empty, read in new buffer
if (i >= length-2)
{
buffer1[0] = buffer1[i];
lastChar1[0] = lastChar1[i++];
cerr << lastChar1[0] << endl;
buffer1[1] = buffer1[i];
lastChar1[1] = lastChar1[i];
count = 2;
i=0;
//while (count < length && (c = getchar()) != EOF )
// buffer1[count++] = c;
while(count < length && !cin.eof())
cin >> buffer1[count++];
if (count < length)
lastChar1[count-2] = true;
}
if (i < length)
{
if(!lastChar1[i])
{
//Convert the character to an int
//Convert the int to a base2 number
if(buffer1[i] != null)
characters = longtobinary((int)(unsigned char)buffer1[i]);
cerr << endl << characters;
//Print out the characters
for (j=0 ; j<8 ; j++)
printNode(&root, ¤t, characters[j]);
}
else if (lastChar1[i] && i != length)
{
if(buffer1[i] != null)
characters = longtobinary((int)(unsigned char)buffer1[i]);
if(buffer1[++i] != null)
lastChar = longtobinary((int)(unsigned char)buffer1[i]);
cerr << endl << characters;
cerr << endl << lastChar;
for (j=0 ; j<8 ; j++){
if(lastChar[j] == '1')
printNode(&root, ¤t, characters[j]);
}
break;
}
}
i++;
}
Oh yeah, everywhere stuff is being printed to cerr is JUST for debugging/testing purposes. BTW, this code seems to work for all cases where the char being read in isn't NUL.
Last edited by TheXenocide; May 13th, 2008 at 07:05 PM.
-
May 13th, 2008, 07:25 PM
#6
Re: cin NUL
What happens if you use:
Code:
while(count < length && cin.good())
{
buffer1[count++] = cin.get();
if (buffer1[count-1] == '\0')
cerr << "Found a null character.";
}
How do you know there are nulls in the data?
-
May 13th, 2008, 07:47 PM
#7
Re: cin NUL
Thats a good suggestion. I'll test it shortly.
Thanks,
Steve
EDIT: That seems to work. The problem is that NUL and NULL are both zero, and when I compare the buffer1 char to NULL (which it is initialized to) it returns true when buffer1 is NUL.
Thanks again jlou (+rep),
Steve
Last edited by TheXenocide; May 13th, 2008 at 08:04 PM.
-
May 13th, 2008, 08:52 PM
#8
Re: cin NUL
Heh, at last, I can post...
Don't use >> and << for character-by-character I/O. They are for parsing and formatting.
Use get() or read() for input. Use put() or write() for output. Then you'll be able to handle null characters without any problems.
Be aware, however, that on Win32 systems if you don't open the file in binary mode that the end of line character sequence CR LF (that's "\r\n") will be translated to/from "\n".
Hope this helps.
-
May 13th, 2008, 10:13 PM
#9
Re: cin NUL
Which is why it's always a good idea to open files in binary mode if you're doing anything other than simply formatted IO.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|