reverse the string sequence in a string buffer
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: reverse the string sequence in a string buffer

  1. #1
    Join Date
    Apr 2013
    Posts
    34

    reverse the string sequence in a string buffer

    hello again! I made a simple little program that takes text from the clipboard, and spits out all the lines I've copied one at a time (so my program can analyze it).

    everything works perfectly, except, it spits it own in the wrong order, I want it to spit out from bottom to top. but currently it spits out the data from top to bottom. here is my code :

    Code:
    #include <iostream>
    #include <Windows.h>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
         HANDLE clip; // assigns the var. clip to hold a handle ID.
    
         if (OpenClipboard(NULL)) // checks to see if there is data in the clipboard & opens it
         {
              clip = GetClipboardData(CF_TEXT); // gets the handle of the clipboard.
              CloseClipboard(); //closes the clipboard (be tidy! lol)
         }
    
         string text; // make the var "text" string type.
         text = (char*)clip; // grabs the strings from the clipboard and stores it in "text"
         cout << (char*)clip << endl << endl; // shows me whats currently in the clipboard
    
         std::istringstream f(text); // splits the string into it's seperate lines.
         std::string line; // <- the variable that will hold the current line
         
         while (std::getline(f, line)) // Uses sstream **** I want this loop to run backwards! ******
         {
              std::cout << line << std::endl; //show the current line to be inspected
              // ADD DIAGNOSING CODE HERE.
              Sleep(1000); // makes it wait a bit before posting the line so I can see it one by one.
         }
    
         Sleep(4000);
    
    }
    *edited for clarification purposes


    the **** I want this loop to run backwards! ****** part is, like it say's what I want to work backwards.
    where as I know how to flip a while loop like this: while(x < 5), I have No idea how to flip a loop like the one I'm using.

    anyone have any Idea's how I can do this?
    thank you!
    Last edited by peteandperry; April 27th, 2013 at 02:38 PM.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,557

    Re: reverse the string sequence in a string buffer

    You're used the wrong code tags! They should use [] and not <>. Also the code should be properly formatted before posting so that it's easier to read.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2013
    Posts
    34

    Re: reverse the string sequence in a string buffer

    *This post was deleted because apparently I'm as blind as a blind bat >.< lol sorry!
    Last edited by peteandperry; April 27th, 2013 at 02:39 PM.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,557

    Re: reverse the string sequence in a string buffer

    *is there seriously no way to edit threads??*
    For your own posts, at the bottom of the post click 'Edit Post'. Also, to use code tags you can also Go Advanced, select code then click '#'.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2013
    Posts
    34

    Re: reverse the string sequence in a string buffer

    sorry, I must not have been signed in or something because I swear I didn't see it anywhere before, again sorry, Fixed now. thank you, I'll remember all this for next time

    but my question still remains, anyone know how to flip a while loop, or flip the string sequence it's looping from? (either one will work)

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: reverse the string sequence in a string buffer

    Do you mean that you want the loop to start with the last line in the file and then run for each line backwards? If so I don't know of any way of doing that. After all linefeed is just a character in the file so how would anybody know where they are before reading the file?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,557

    Re: reverse the string sequence in a string buffer

    As GetClipBoardData returns a c-style null terminated string, perhaps the easiest way is to use strrchr(..) as below

    Code:
    #include <iostream>
    #include <Windows.h>
    
    using namespace std;
    
    int main()
    {
    	//Checks to see if there is data in the clipboard & opens it
    	if (!OpenClipboard(NULL)) {
    		cout << "Open error: " << GetLastError() << endl;
    		return 1;
    	}
    
    HANDLE	clip;
    
    	//Get the handle of the clipboard.
    	if ((clip = GetClipboardData(CF_TEXT)) == NULL) {
    		cout << "Get data error: " << GetLastError() << endl;
    		return 2;
    	}
    
    	CloseClipboard(); //closes the clipboard (be tidy! lol)
    
    LPSTR	str,
    	str1; 
    
    	if ((str = (LPSTR)GlobalLock(clip)) != NULL) {
    		while ((str1 = strrchr(str, '\n')) != NULL) {
    			cout << str1 + 1 << endl;     // or assign it to a string or use as needed
    			*str1 = 0;
    		}
    		cout << str << endl;
    		GlobalUnlock(clip);
    	}
    	return 0;
    }
    Last edited by 2kaud; April 27th, 2013 at 03:46 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 2013
    Posts
    34

    Re: reverse the string sequence in a string buffer

    S_M_A: I was afraid of that, ah well thank you for your reply!

    2kaud: THAT'S PERFECT! seriously, thank you so much! I was really stumped there for a while.
    though, out of curiosity.. (you don't have to answer, you already helped save a few days of my time, and that's enough)

    why do I see so many people in c++ write statements like this:
    Code:
    if (x < y) {
       // stuff
    }
    rather then this (the way I'm use to, and makes more sense to me)
    Code:
    if (x < y)
    {
       //stuff
    }
    as well as, why always finish with "return 0" even though no other part of my program is going to check for that.

    again, no need to reply, just curios.

    and again, THANK YOU! you are an awesome human being! and I hope life treats you awesome for it.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,557

    Re: reverse the string sequence in a string buffer

    why always finish with "return 0"
    main() returns an int. By convention 0 is returned as meaning no error, with other values meaning an error had occured. Thats why I returned 1 or 2 depending upon the error. Though no part of your program is going to check it, if your program is used in, say a batch file, then the return code could be checked as part of the batch file and different actions taken depending upon the return value. If in the function main no return is used then the compiler assumes a return 0. main() is the only function with a return value where the return is not mandatory. IMO I don't like not putting the return 0 in as it "doesn't look right". Also if your main function always returns a value then it's easier to turn it into a function with a different name that compiles without giving a load of warnings/errors about not returning a value.

    As for the way of writing block statements, it's mostly a matter of preference (and conformance to house style!) There's really no 'right' way of doing it but once you use a particular way then you should be consistent. I don't like single line if/when statements etc as it's too easy to add extra lines into what you think is a block because it's indented when it isn't! So when I write one of these statements I always put a { after the final closing ).
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center