VB icon

Save Clipboard text

Email
Submitted on: 8/21/2016 1:42:18 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++, UNIX C++
Views: 879
 
     A small tool to save any text that might be on the clipboard to a ooutput file or to the console STDOUT hope you find this code usfull.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Save Clipboard text
// Description:A small tool to save any text that might be on the clipboard to a ooutput file or to the console STDOUT hope you find this code usfull.
// By: Ben
//**************************************

/*
	SaveClip By DreamVB
	Version 1.0
*/
#include <iostream>
#include <stdio.h>
#include <Windows.h>
using namespace std;
using std::cout;
using std::endl;
void DisplayErrror(char *msg){
	cout << msg << endl;
}
int main(int argc, char* argv[]){
	char * lzBuffer = NULL;
	FILE *fout = NULL;
	HANDLE hClip;
	if(argc <=1){
		//The data will be writen to the concole.
		fout = stdout;
	}else{
		//Check for help flag
		if(argv[1][0]=='/'){
			if(argv[1][1] != '?'){
				DisplayErrror("Invaild flag.");
			}
			else{
				//Display help
				cout << "Usage: " << argv[0] << " [Outputfile]" << endl;
			}
			return 0;
		}else
		{
			//Data will be writen to filename.
			fout = fopen(argv[1],"w");
			if(!fout){
				DisplayErrror("IO/Error cannot write to output file.");
				return 0;
			}
		}
	}
	//Try and open the clipboard.
	if(!OpenClipboard(NULL)){
		DisplayErrror("Cannot open clipboard.");
		return 0;
	}
	//Try and open clipboard data for Text Format
	hClip = GetClipboardData(CF_TEXT);
	
	//Check for NULL
	if(hClip == NULL){
		DisplayErrror("Cannot open clipboard data.");
		return 0;
	}
	//Fetch clipbpard contents into lzBuffer
	lzBuffer = static_cast<char*>( GlobalLock(hClip) );
	//Check we have data.
	if(lzBuffer != NULL){
		//Write to fout
		fprintf(fout,lzBuffer);
	}
	//Tidy up time.
	GlobalUnlock(hClip);
	CloseClipboard();
	//Close file
	fclose(fout);
	return 1;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.