从从 & 开头然后包含 23 个字符的文件中删除字符串
Remove a String from a file which starts from a & and then has 23 characters in it
我正在开发一个小程序来实现一些目标。
因此,我使用该程序获取了一个文件,但它有一个以"&"开头的字符串在很多地方。
我将解释我问题中最细微的部分。我有一个字符串,在这个字符串中我想删除一些字符,它以'&'开头后面跟着23位数字。
请建议我如何做到这一点。
好吧,您需要创建一个循环来读取文件中的所有字符串:
std::ifstream fin( "input.txt" );
std::string line;
std::getline( fin, line );
while( !fin.eof() ) {
getline( fin, line );
}
当然,您不能就地修改文件。您需要将输入文件的内容写入另一个文件中。
std::ifstream fin( "input.txt" );
std::ofstream fout( "output.txt" );
std::string line;
std::getline( fin, line );
while( !fin.eof() ) {
fout << line << std::endl;
getline( fin, line );
}
剩下的唯一一件事就是找到那些带有"&"的字符串并消除随后的23个字符。
std::ifstream fin( "input.txt" );
std::ofstream fout( "output.txt" );
std::string line;
std::getline( fin, line );
while( !fin.eof() ) {
unsigned int pos = line.find( '&' );
if ( pos != string::npos ) {
string line2 = line.substring( 0, pos -1 );
line2 += line.substring( pos + 23 );
line = line2;
}
fout << line << std::endl;
std::getline( fin, line );
}
最后,您需要去掉input.txt
。希望这能有所帮助。
您应该使用string.find和string.replace.
这里的代码示例http://www.cplusplus.com/reference/string/string/find/显示了一种完美的方法:)
std::string content = "some long string";
std::string string_to_replace = "some ";
str.replace(content.find(string_to_replace),string_to_replace.length(),"");
这将在内容中找到string_to_replace,并将其替换为"。当然,这是假设您在写入文件之前解决了问题。在本例中,它将产生"长字符串"。
相关文章:
- 如何在我从C++文件中读取后立即删除行
- 删除 XML 文件中的子节点C++
- 如何在输入新内容 c++ 之前删除文件中的所有内容
- 如何更新和删除文本文件中的特定逗号分隔值?C++
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 文件模式标志"ios::app"是否用于删除文件(如果文件已存在)?
- 如何自动获取我的项目的路径并删除一些文件
- C++ VS 错误:<实验/文件系统>提供 std::experimental::文件系统的标头已被Microsoft弃用,将被删除
- 如果我删除指针,我的C++程序会意外删除系统文件吗?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- 文件删除(使用取消链接)与释放所有磁盘空间之间的延迟
- 重命名批处理文件(删除名称中的一些符号)(python/matlab/shell)
- 如何找到字符串并使用C 文件中的文件删除它
- 使用 DEF 文件删除 dll 导出的名称
- 关闭后等待文件删除
- c++中的文件删除
- 如何共享文件删除权限时,我打开一个文件ifstream
- 典型的Win32文件删除(IDropTarget::drop)实现
- 生成文件模式规则:循环生成文件.o < - 删除生成文件依赖项