-
April 25th, 2014, 11:49 AM
#1
Piped text to binary
Hi Guys
Quick question.
What do you think the best way is to break up pipped data coming into a C++ programming from command line into x number of bytes (eg. 5 bytes at a time) for manipulation?
I have the following at the moment, but not sure if this is correct and how to handle the tail of the message where i may have less than 5 bytes / characters in the data block.
Code:
while (!cin.eof())
{
int size = 5;
char *buffer = new char[1];
buffer[0] = '\0';
cout << buffer << "\n";
cin.read(buffer, size);
eg. 1234567
Would show me
12345
67
45
The 45 should not be there!
Thanks
-
April 25th, 2014, 12:00 PM
#2
Re: Piped text to binary
1) You are only allocating 1 space in buffer, instead of 5
2) In while loops, checking eof() almost always leads to
outputting extra values, since eof() does not become true
until an actual attempt to read past eof is made.
3) A safer way:
Code:
while (cin.read(buffer, size))
{
// do something with buffer.
// you can use the gcount() member function
// to see how many characters were read
}
-
April 25th, 2014, 12:13 PM
#3
Re: Piped text to binary
Hi,
With the following method, for "1234567"
I would only see 12345
67 isn't shown.
Thanks
Code:
char *buffer = new char[5];
while (cin.read(buffer, 5))
{
cout << buffer << "\n";
}
-
April 25th, 2014, 12:32 PM
#4
Re: Piped text to binary
Try this
Code:
const int NOBYTES = 5;
char buffer[NOBYTES + 1] = {0};
while (cin.get(buffer, NOBYTES + 1))
cout << buffer << endl;
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2014, 12:50 PM
#5
Re: Piped text to binary
Hi
The last example works great - but only for 1 line.
I'm sorry if my example data was misleading.
I could have something like:
1234567
Hello This Is
A Test
I would need to ready 5 characters aka bytes at a time.
Thanks
-
April 25th, 2014, 01:06 PM
#6
Re: Piped text to binary
How do you want to deal with <LF>? Do you want the above to show as
Code:
12345
67Hel
lo Th
Is A
Test
or
Code:
12345
67
Hello
This
Is
A Tes
t
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2014, 01:14 PM
#7
-
April 25th, 2014, 01:45 PM
#8
Re: Piped text to binary
OK. Try this
Code:
const int NOBYTES = 5;
string cline;
while (getline(cin, cline))
for (size_t pos = 0; pos < cline.length(); pos += NOBYTES)
cout << cline.substr(pos, NOBYTES) << endl;
Last edited by 2kaud; April 25th, 2014 at 02:11 PM.
Reason: Code simplification
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2014, 04:17 PM
#9
Re: Piped text to binary
This works.
I need to do manipulation with the chucks of strings. Isnt it best to use chars to do this? I need to mangle the block using bit operation.
-
April 25th, 2014, 04:25 PM
#10
Re: Piped text to binary
Originally Posted by rudyloo
This works.
I need to do manipulation with the chucks of strings. Isnt it best to use chars to do this? I need to mangle the block using bit operation.
You can acess the chars in the string using iterators or [] or if you really must, use the class function .c_str() to return a pointer of type const char*. See http://www.cplusplus.com/reference/string/string/
What exactly are you wanting to do with the blocks of text?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2014, 04:31 PM
#11
Re: Piped text to binary
I want to read 5 characters in at a time, and then XOR it with a password of 5 characters long. And then output the mangled block of 5 characters to file or screen.
So, I would want to retain all the special characters as well. eg. \n \t spaces etc..
Eg. XOR each character of "ABCDE" with "MYWORD". A AND M, and then B and Y, and then C and W etc..
-
April 25th, 2014, 04:43 PM
#12
Re: Piped text to binary
MYWORD is 6 chars!
You want to leave 'whitespace' chars un-mangled and xor the others. What about other special chars such as punctuation etc or do you only want to mangle letters - what about numbers?
PS This isn't an assignment is it?
Last edited by 2kaud; April 25th, 2014 at 04:46 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2014, 04:55 PM
#13
Re: Piped text to binary
From what I understand ASCII is a subset to Unicode. I would ned to handle anything in the ASCII table. So I assume this means spaces, and numbers.
-
April 25th, 2014, 05:13 PM
#14
Re: Piped text to binary
This will mangle all text entered. The string mangle must be at least NOBYTES in length.
Code:
const string mangle = "MYXOR";
const int NOBYTES = 5;
string cline;
while (getline(cin, cline))
for (size_t pos = 0; pos < cline.length(); pos += NOBYTES) {
string mang = cline.substr(pos, NOBYTES);
for (size_t m = 0; m < mang.length(); ++m)
mang[m] ^= mangle[m];
cout << mang << endl;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2014, 08:50 PM
#15
Re: Piped text to binary
Hi, thanks for all your help.
I have it reading line by line and then XORing char by char given the mangle string.
I can output the encrypted file fine. However re-encrypting (decrypting) the encrypted file doenst give me the original piped in data. (Which it should).
I am thinking when I XOR, I probably get non standard characters (letters, numbers, basic symbols), and probably have \n \0 in the encrypted data. Therefore when I try to reprocess the encrypted data, it processes the data line by line - which doesn't match up.
So, perhaps reading 1 line at a time isn't the way to go.
Thoughts?
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
|