在 C++ 中剪切文件中的单词和书写,同时保留行号
cutting words in a file and writing while preserving line number in c++
#include<fstream>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
ifstream infile;
ofstream outfile;
infile.open("oldfile.txt");
outfile.open("newfile.txt");
while(infile){
string str,nstr;
infile>>str;
char charr[10];
charr[0]='<';charr[1]=' ';
nstr=str.substr(0,str.find_first_of(charr));
outfile<<nstr<<' ';
}
}
该程序使用 substr(0, string.find_first-of(字符数组,它是子字符串的起点))每个单词不必要的子字符串,但在写入另一个文件时它不会保留行号。 你能解决它吗. 它按顺序逐字写入文件。 代码没有逐行保留,
字符串
输入不关心行边界,它将,\t,\v和其他可能与空格相同。
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line,word;
char foo[] = "<";
while ( getline(cin,line) ) {
string newline;
for ( istringstream words(line)
; words >> word ; ) {
newline+=word.substr(0,word.find_first_of(foo))+' ';
}
cout << newline << 'n';
}
}
更改
outfile<<nstr<<' ';
自
outfile<<nstr<<endl;
这将逐行写入,而不是使用单个空格字符。
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 为多个会话保留XPtr
- 保留对其他类的成员函数的引用
- 指针保留字符串
- 是否有内置方法可以强制转换为不同的基础类型,但保留常量限定符?
- 如何让 GCC/Clang 在保留标识符上出错
- 必须为 C++20 协程帧保留多少内存?
- 如何将一个窗口保留在另一个应用程序窗口的前面
- 使用 char 分隔符解析C++中的字符串,但将可重复的字符保留为每个解析的子字符串 (C++ STL) 中的分隔符
- 局部变量保留函数中的值
- 保留函数指针模板参数
- 变量超出范围后如何保留向量值?
- C++矢量复制构造函数和赋值运算符是否也复制保留空间?
- 当为可变性配置时,boost::heap::d_ary_heap 保留的额外 std::list 的目的是什么?
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 即使在使用 delete[] 后仍保留的元素
- 如何在成为指向基类的指针后保留对子类方法的使用?
- 将成员函数保留为未定义
- C++:如何为多个重载函数保留通用代码路径?
- 在 C++ 中剪切文件中的单词和书写,同时保留行号