![](/img/trans.png)
[英]c++ aligning the same word in a text file in different lines/positions to the same position in each line
[英]C++ / C: Trim the first word of each line of a text file
我正在尋找一個C / C ++甚至C#代碼,它們將修剪文本文件中每行的第一個單詞
例如file.txt
test C:\Windows\System32\cacl.exe
download C:\Program Files\MS\
因此,我將剩下:
C:\Windows\System32\cacl.exe
C:\Program Files\MS\
我有當前代碼,但它似乎不起作用:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char s[2048];
while (fgets(s, sizeof(s), stdin))
{
char *pos = strpbrk(s, "|\r\n");
if (pos != 0)
fputs(pos+1, stdout);
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
string tmp;
while ( !cin.eof() )
{
cin >> tmp;
getline(cin, tmp);
cout << tmp << endl;
}
}
C#:
var lines = File.ReadAllLines("...");
var removedFirstWords = from line in lines
select line.SubString(line.IndexOf(" ")+1);
(未檢查。可能包含錯誤)
在C#中:
var fileContent = File.ReadAllText(@"c:\1.txt");
var result = Regex.Replace(fileContent, @"^\w*\s+(.*)$", "$1", RegexOptions.Multiline);
File.WriteAllText(@"c:\2.txt", result);
C#:-
string line = "test C:\Windows\System32\cacl.exe";
string output = line.substring(line.IndexOf(" "));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.