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!
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.
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!
Re: reverse the string sequence in a string buffer
Quote:
*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 '#'.
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)
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?
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;
}
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.
Re: reverse the string sequence in a string buffer
Quote:
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 ).