C++/C : Trim the first word of each line of a text file

标签 c++ c string

我正在寻找一个 C/C++ 甚至 C# 代码来修剪文本文件中每一行的第一个单词

例如文件.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++/C : Trim the first word of each line of a text file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6084027/

相关文章:

c++ - 使用 linux 操作内存中的位图图像

c++ - 枚举声明中枚举器的类型

C++ 类 : Pass by reference

c - 如何计算嵌套for循环最内层循环的迭代总数?有什么公式吗?

c++ - win32 C/C++ 从一个 "locked"文件中读取数据

c - 定义#define宏

c++ - 黑/白 PRLock 和 PRRWLock 有什么区别

string - 如何将 Tcl 列表框数字索引转换为其元素

javascript - 对字符串中的元素进行排序

Java 打印字符串 C++ 等价物