首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >串流线处理

串流线处理
EN

Stack Overflow用户
提问于 2018-03-22 11:13:18
回答 1查看 403关注 0票数 0

下面的代码将文本文件复制到向量中,然后将文本文件中的所有行流到剪贴板。

我想将文本文件复制到一个向量中,并对所有的行进行流,但是我想在下一次使用////code..处理每一行之前,我正在为循环逻辑而挣扎。

E.G

第1行加载到流中并复制到剪贴板,现在对第1行执行一些操作。

next>

第2行加载到流中并复制到剪贴板,现在对第2行执行一些操作,等等。

input.txt看起来像这个

代码语言:javascript
复制
000000
000001
000002
000003
000004
000005
000006
000007
000008

因此,将000000复制到剪贴板,在000000上运行////code..,然后对000001000002000003000004.....and等做同样的操作。

如有任何建议,将不胜感激。

下面的代码:

代码语言:javascript
复制
#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"));

        ////code..
        //Do something to line 1 of stream
        //Do something to line 2 of stream
        //Do something to line 3 of stream
        //.....
        //

        // 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());
        std::getchar();
    }

    return 0;
} 
EN

回答 1

Stack Overflow用户

发布于 2018-03-22 13:37:16

您只需要定义什么是“做某事”,并使用它来转换每个元素。

代码语言:javascript
复制
std::string DoSomething(std::string line)
{
    return "Some " + line + " Change";
}

int main()
{
    std::vector<std::string> vecOfStr;
    // Get the contents of file in a vector.
    bool result = getFileContent("input.txt", vecOfStr);
    if (result)
    {
        // Transform
        std::transform(vecOfStr.begin(), vecOfStr.end(), vecOfStr.begin(), DoSomething);

        // Populate & Display
        std::stringstream ss;
        std::copy(vecOfStr.begin(), vecOfStr.end(), std::ostream_iterator<std::string>(ss, "\n"));
        std::copy(vecOfStr.begin(), vecOfStr.end(), std::ostream_iterator<std::string>(std::cout, "\n"));

        // 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());
        std::getchar();
    }

    return 0;
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49427212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档