我想用 500 多行翻转文档的行顺序。这些行不仅仅是数字,有些还包括文本和其他字符。这是一个混合体。
例子:
1号线 2号线 3号线 4号线 5号线 6号线
然后我想翻转,反转并从下到上看起来像这样:
6号线 5号线 4号线 3号线 2号线 1号线
Gnu*_*bie 116
除了通常包含的 TextFX 插件之外,不需要其他软件的解决方案:
Tre*_*ung 53
这也可以在没有 TextFX 插件的 Notepad++ 中完成。它遵循与已接受答案相同的策略,但使用本机功能。操作如下:
好吧,既然我们给出了代码示例,如果您使用的是 Windows 7 或者您已经在其他版本的 Windows 上安装了 PowerShell,那么:
$foo = New-Object System.collections.arraylist;
$foo.AddRange($(Get-Content 'C:\Path\To\File.txt));
$foo.Reverse();
$foo | Out-File C:\Path\To\File.txt
Run Code Online (Sandbox Code Playgroud)
或者对于非编码答案,下载gVim,打开文件并输入:
:g/^/m0
Run Code Online (Sandbox Code Playgroud)
如果您对编译 C++ 感到舒服,这应该可以解决问题。基本上,我将文件的每一行放在一个向量中,然后使用反向迭代器将其输出到一个新文件中。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> fileLines;
std::string currLine;
std::ifstream inFile("input.txt");
if (inFile.is_open())
{
while (inFile.good())
{
std::getline(inFile, currLine);
fileLines.push_back(currLine);
}
inFile.close();
}
else
{
std::cout << "Error - could not open input file!\n";
return 1;
}
std::ofstream outFile("output.txt");
if (outFile.is_open())
{
std::vector<std::string>::reverse_iterator rIt;
for (rIt = fileLines.rbegin(); rIt < fileLines.rend(); rIt++)
{
outFile << *rIt;
}
outFile.close();
}
else
{
std::cout << "Error - could not open output file!\n";
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果输出文件丢失字里行间换行符,然后改变outFile << *rIt;
是 outFile << *rIt << "\r\n";
这么一个换行符被添加(省略的\r
,如果你是在Unix / Linux)。
免责声明:我没有测试过这段代码(我在记事本中写得非常快),但它看起来可行。
归档时间: |
|
查看次数: |
79760 次 |
最近记录: |