检测新行c ++ fstream [英] Detect new line c++ fstream
问题描述
如何通过使用fstream将类似的内容复制到另一个.txt中.txt。
问题是,当在文件中有新行。如何检测,而使用ifstream?
用户输入苹果
例如:
note.txt =>
昨天我买了一个苹果。
苹果口味好吃。
$ b $ note_new.txt =>
我昨天买了一个。
味道鲜美。
由此产生的钞票假设在上面,而是:
note_new.txt =>
我买了一个昨天。味道鲜美。
如何检查源文件中是否有新行,还会在新文件中创建新行。 b
以下是我目前的代码:
#include< iostream>
#include< fstream>
#include< string>
使用namespace std;
int main(){
ifstream inFile(note.txt);
字符串字;
ofstream outFile(note_new.txt);
while(inFile>> word){
outfile<<字<< ;
}
}
你们能帮助我吗?其实我也检查检索的单词是什么时候与用户指定的相同,然后我不会在新文件中写入该单词。所以一般来说,它会删除与用户指定的单词相同的单词。 解决方案
逐行方法
如果您仍想逐行执行,您可以使用 std :: getline()
:
#include< iostream>
#include< fstream>
#include< string>
使用namespace std;
int main(){
ifstream inFile(note.txt);
字符串行;
// ^^^^
ofstream outFile(note_new.txt);
while(getline(inFile,line)){
// ^^^^^^^^^^^^^^^^^^^^^
outfile <<行<< ENDL;
$ b $ p
$ b $ p $
$ h
$ b 更容易的方法
如果你只是想重写另一个文件,使用 rdbuf
:
#include< fstream>
使用namespace std;
int main(){
ifstream inFile(note.txt);
ofstream outFile(note_new.txt);
outFile<< inFile.rdbuf();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
}
编辑:允许删除您不想要的字词在新文件中:
我们使用 std :: stringstream
:
#include<的iostream>
#include< fstream>
#include< stringstream>
#include< string>
使用namespace std;
int main(){
ifstream inFile(note.txt);
字符串行;
string wordEntered(apple); //从命令行获取
ofstream outFile(note_new.txt);
while(getline(inFile,line)){
stringstream ls(line);
字符串字;
while(ls>> word)
{
if(word!= wordEntered)
{
outFile<字;
}
}
outFile<< ENDL;
}
}
How do I read a .txt copy the content to another .txt by using fstream to a similar content.
The problem is, when in the file there is new line. How do I detect that while using ifstream?
user enter "apple"
Eg:
note.txt =>
I bought an apple yesterday.
The apple tastes delicious.
note_new.txt =>
I bought an yesterday.
tastes delicious.
the resulting note suppose to be above, but instead:
note_new.txt =>
I bought an yesterday. tastes delicious.
How do I check if there is a new line in the source file, it also will create new line in new file.
Here is my current code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFile ("note.txt");
string word;
ofstream outFile("note_new.txt");
while(inFile >> word) {
outfile << word << " ";
}
}
can you all help me? actually I also check when the word retrieved is the same as what user specified, then I won't write that word in the new file. So it general, it will delete words which are the same as the one specified by user.
解决方案 Line-by-line method
If you still want to do it line-by-line, you can use std::getline()
:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFile ("note.txt");
string line;
// ^^^^
ofstream outFile("note_new.txt");
while( getline(inFile, line) ) {
// ^^^^^^^^^^^^^^^^^^^^^
outfile << line << endl;
}
}
It gets a line from the stream and you just to rewrite it wherever you want.
Easier method
If you just want to rewrite one file inside the other one, use rdbuf
:
#include <fstream>
using namespace std;
int main() {
ifstream inFile ("note.txt");
ofstream outFile("note_new.txt");
outFile << inFile.rdbuf();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
}
EDIT : It will permit to remove the words you don't want to be in the new file :
We use std::stringstream
:
#include <iostream>
#include <fstream>
#include <stringstream>
#include <string>
using namespace std;
int main() {
ifstream inFile ("note.txt");
string line;
string wordEntered("apple"); // Get it from the command line
ofstream outFile("note_new.txt");
while( getline(inFile, line) ) {
stringstream ls( line );
string word;
while(ls >> word)
{
if (word != wordEntered)
{
outFile << word;
}
}
outFile << endl;
}
}
这篇关于检测新行c ++ fstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!