【问题标题】:Process file lines stream iterator处理文件行流迭代器
【发布时间】:2018-08-29 06:39:16
【问题描述】:

嗨,我在下面有这个脚本,它从input.txt 逐行传输所有数据 完成后,流数据将被复制到剪贴板。

我想流式传输一行流程并复制该行,然后流式传输并复制下一行......等等。

E.G:

打开input.txt将第一行流式复制到剪贴板,运行鼠标单击宏,运行粘贴宏

然后

将第二条流式传输线复制到剪贴板,运行鼠标单击宏,运行粘贴宏...

循环这个input.txt

我已经在迭代器流中使用/n作为分隔符来分隔每一行

// 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;
}

【问题讨论】:

    标签: c++ windows loops iterable


    【解决方案1】:

    这一行让您的程序一次性处理文件的所有行(现在存储为向量中的元素)。

    std::copy(vecOfStr.begin(), vecOfStr.end(), std::ostream_iterator<std::string>(ss, "\n"));
    

    要使其逐行工作,只需使用for 循环单独迭代存储向量的元素。

    for (auto str : vecOfStr)
        {
        toClipboard(hwnd, str);
        // run mouse click macro, run paste macro...
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多