【问题标题】:Copy vector to clipboard将矢量复制到剪贴板
【发布时间】:2018-03-18 16:05:43
【问题描述】:

您好,我的目标是将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

input.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;
}

【问题讨论】:

  • ups - 对不起。好像您删除了原始文件并将其全部替换。我又把它卷了回去。很抱歉造成混乱,一切都很好

标签: c++ string windows file vector


【解决方案1】:

忽略对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 并将矢量打印到该位置,然后将 that 复制到剪贴板。

【讨论】:

  • 我还有一个简短的问题。如何一次在input.txt 中流式传输一行?例如:流The复制到剪贴板并等待1s,然后流Brown复制到剪贴板并等待1s.....等等?
  • @MALKAVIAN 这个问题可能很快,但答案可能不是。我相信您可以将其作为一个新问题提出。
  • @MALKAVIAN:正是你描述它的方式。编程就是写下一系列步骤。您已经定义了每个步骤,所以剩下的就是将其放入 C++ 中。你可能想看看std::this_thread::sleep_for()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 1970-01-01
  • 2020-02-06
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多