CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jan 2007
    Posts
    97

    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

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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
    }

  3. #3
    Join Date
    Jan 2007
    Posts
    97

    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";
     }

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  5. #5
    Join Date
    Jan 2007
    Posts
    97

    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

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  7. #7
    Join Date
    Jan 2007
    Posts
    97

    Re: Piped text to binary

    Bottom version.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  9. #9
    Join Date
    Jan 2007
    Posts
    97

    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.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Piped text to binary

    Quote Originally Posted by rudyloo View Post
    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)

  11. #11
    Join Date
    Jan 2007
    Posts
    97

    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..

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  13. #13
    Join Date
    Jan 2007
    Posts
    97

    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.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  15. #15
    Join Date
    Jan 2007
    Posts
    97

    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?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured