[英]Stringstream Line Processing
下面的代码将文本文件复制到向量中,然后将文本文件中的所有行流式传输到剪贴板。
我想将文本文件复制到向量中,并流所有要使用的行,但我想用////code..
处理每一行,然后再尝试使用循环逻辑。
例如
第1行已加载到流中并复制到剪贴板,现在对第1行做一些操作
下一个>
第2行被加载到流中并复制到剪贴板,现在对第2行做一些操作,依此类推。
input.txt
看起来像这样:
000000
000001
000002
000003
000004
000005
000006
000007
000008
因此,采取000000
复制到剪贴板运行////code..
上000000
,然后做同样的000001
, 000002
, 000003
, 000004
.....等等。
任何建议将不胜感激,谢谢。
下面的代码:
#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;
}
您只需要定义“ Do Something”是什么,然后使用它来转换每个元素。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.