复制矢量到剪贴板

时间:2018-03-18 16:05:43

标签: c++ string windows file vector

我的目标是让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;
}

1 个答案:

答案 0 :(得分:2)

忽略对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);
    }

您有一个名为string的默认构造line,并将其复制到剪贴板。毫不奇怪,它不包含任何字符。我认为你需要创建一个stringstream并打印向量,然后将那个复制到剪贴板。