嗨,我的目标是将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;
}

最佳答案

忽略所有与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并将其打印到矢量,然后将其复制到剪贴板。

10-04 21:56