【问题标题】:Stringstream Line Processing字符串流线处理
【发布时间】:2018-03-22 11:13:18
【问题描述】:

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

我想将文本文件复制到一个向量中并流式传输所有行但我想在下一个循环逻辑之前用////code.. 处理每一行。

E.G

第 1 行被加载到流中并复制到剪贴板,现在对第 1 行做一些事情

下一个>

第 2 行被加载到流中并复制到剪贴板,现在对第 2 行做一些事情 等等..


input.txt 看起来像这样 :

000000
000001
000002
000003
000004
000005
000006
000007
000008

所以将000000 复制到剪贴板,在000000 上运行////code..,然后对000001000002000003000004......等执行相同操作。

任何建议将不胜感激。

代码如下:

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

【问题讨论】:

  • 据我了解,您想对输入进行一些处理-从文本文件中读取-然后将处理后的数据发送到剪贴板?对?您拥有字符串向量中的所有数据。向量中的每个元素都是文件中的一行,你为什么不循环你的向量——在复制到流之前——做处理然后复制它?
  • 000000上运行////code..是什么意思?
  • @super ////code.. 表示字符串处理,如添加_1, 或仅复制行000000 或仅复制行000003
  • @MALKAVIAN 好的,你怎么知道如何处理某行?我猜你对不同的线路做不同的事情。

标签: c++ arrays string


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-01-21
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多