将矢量复制到剪贴板
Copy vector to clipboard
嗨,我的目标是从input.txt
中获取每一行,将它们加载到一个向量中,然后每 1 秒将每个向量复制到剪贴板。
到目前为止,我能够使用 getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)
将文件加载到矢量中
如果我替换以下内容,我还可以将字符串复制到剪贴板:
cout << "Lines Copying " << endl;
跟
cout << "Please enter sentence: "; cin >> AAA;
使用用户输入...
但是,当我尝试加载名为line
的向量时,我0 char(s)
复制到剪贴板?
我做错了什么?任何指示或建议将不胜感激。
用:
Windows 10 64x
Microsoft Visual Studio Community 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.7.02046
Visual C++ 2015 00322-20000-00000-AA355
Microsoft Visual C++ 2015
输入.txt:
The
Brown
Fox
Jumps
脚本:
// copyfilelines.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <vector>
#include <direct.h>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <winuser.h>
#include <cmath>
#include <iomanip>
#include <complex>
void toClipboard(HWND hwnd, const std::string &s);
/*
* It will iterate through all the lines in file and
* put them in given vector then copy vector to clipboard.
*/
//1. Open file and put each line into a vector.
bool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)
{
// Open the File
std::ifstream in(fileName.c_str());
// Check if object is valid.
if (!in)
{
std::cerr << "Cannot open the File : " << fileName << std::endl;
return false;
}
std::string str;
// Read the next line from File untill it reaches the end.
while (std::getline(in, str))
{
// Line contains string of length > 0 then save it in vector.
if (str.size() > 0)
vecOfStrs.push_back(str);
}
// Close The File.
in.close();
return true;
}
//2. Declare clipboard functions at file scope.
void toClipboard(HWND hwnd, const std::string &s) {
OpenClipboard(hwnd);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size() + 1);
if (!hg) {
CloseClipboard();
return;
}
memcpy(GlobalLock(hg), s.c_str(), s.size() + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
}
int main()
{
std::vector<std::string> vecOfStr;
// Get the contents of file in a vector.
bool result = getFileContent("input.txt", vecOfStr);
if (result)
{
// Print the vector contents.
for (std::string & line : vecOfStr)
std::cout << line << std::endl;
// Copy vector to clipboard.
using namespace std;
string line;
cout << endl;
cout << endl;
cout << "Lines Copying " << endl;
cout << endl;
cout << endl;
cout << "Lines Copied To The Clipboard: ";
cout << endl;
cout << line << endl;
// 1. Strlen takes a const char*, so have to call the strings c_str() method
// (but it would be better to use len = line.length() instead)
size_t len = strlen(line.c_str());
cout << len << " char(s)" << endl;
// Get desktop windows and the call toClipboard.
HWND hwnd = GetDesktopWindow();
toClipboard(hwnd, line);
//User input processing.
//cin.clear();
//cin.ignore(255, 'n');
//cin.get();
}
return 0;
}
编辑:
更新的脚本
我添加了一个std::ostream_iterator
以允许流式传输矢量:
std::stringstream ss;
// Populate
std::copy(vecOfStr.begin(), vecOfStr.end(), std::ostream_iterator<std::string>(ss, "n"));
// Display
std::cout << ss.str() << std::endl;
// Copy vector to clipboard.
size_t len = strlen(ss.str().c_str());
完整更新的脚本:
流式传输input.txt
的内容,并将所有流式传输的内容复制到剪贴板。
// copyfilelines.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <fstream>
#include <string>
#include <vector>
#include <direct.h>
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <winuser.h>
#include <cmath>
#include <iomanip>
#include <complex>
#include <iostream>
#include <sstream>
#include <iterator>
void toClipboard(HWND hwnd, const std::string &s);
/*
* It will iterate through all the lines in file and
* put them in given vector then copy vector to clipboard.
*/
//1. Open file and put each line into a vector.
bool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)
{
// Open the File
std::ifstream in(fileName.c_str());
// Check if object is valid.
if (!in)
{
std::cerr << "Cannot open the File : " << fileName << std::endl;
return false;
}
std::string str;
// Read the next line from File untill it reaches the end.
while (std::getline(in, str))
{
// Line contains string of length > 0 then save it in vector.
if (str.size() > 0)
vecOfStrs.push_back(str);
}
// Close The File.
in.close();
return true;
}
//2. Declare clipboard functions at file scope.
void toClipboard(HWND hwnd, const std::string &s) {
OpenClipboard(hwnd);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size() + 1);
if (!hg) {
CloseClipboard();
return;
}
memcpy(GlobalLock(hg), s.c_str(), s.size() + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
}
int main()
{
std::vector<std::string> vecOfStr;
// Get the contents of file in a vector.
bool result = getFileContent("input.txt", vecOfStr);
if (result)
{
std::stringstream ss;
// Populate
std::copy(vecOfStr.begin(), vecOfStr.end(), std::ostream_iterator<std::string>(ss, "n"));
// Display
std::cout << ss.str() << std::endl;
// Copy vector to clipboard.
size_t len = strlen(ss.str().c_str());
// Get desktop windows and the call toClipboard.
HWND hwnd = GetDesktopWindow();
toClipboard(hwnd, ss.str());
Sleep(100000);
}
return 0;
}
忽略
所有不相关的打印到cout
,你的代码是:
if (result)
{
// Copy vector to clipboard.
using namespace std;
string line;
size_t len = strlen(line.c_str());
// Get desktop windows and the call toClipboard.
HWND hwnd = GetDesktopWindow();
toClipboard(hwnd, line);
}
您有一个名为 line
的默认构造string
,并将其复制到剪贴板。 毫不奇怪,它不包含任何字符。 我认为您需要创建一个stringstream
并将矢量打印到该,然后将其复制到剪贴板。
相关文章:
- C++剪贴板队列粘贴随机结果?
- 将缓冲区复制到剪贴板 [换行错误]
- wxWidgets mac剪贴板在3.1.3上坏了?
- 获取剪贴板数据(CF_HDROP)在剪切和粘贴中失败
- 将矢量复制到剪贴板
- Linux/C++ 将字符串复制到剪贴板
- 从表视图复制到剪贴板
- 无法将 HTML 格式的 unicode(wchar_t 使用)复制到剪贴板
- BitBlt从剪贴板复制位图会给出错误的颜色
- 将文本复制到 MFC 中的剪贴板
- 如何将字符数组复制到剪贴板
- 通过QT将(rtf)表复制到剪贴板(或:将QTextDocument写入剪贴板)
- 将 unicode 字符串复制到剪贴板不起作用
- 如何在qt的剪贴板上复制lineEdit的内容
- 复制和粘贴剪贴板从不同的应用程序
- 在c++中复制到剪贴板
- 如何在C++中从剪贴板复制/粘贴
- 将 QML 项目作为图像复制到剪贴板C++
- C++:如何将多个对象复制到剪贴板并在之后提取它们
- C++ WINAPI 按钮剪贴板 - 复制操作