C++ 检测新线c++;fstream

C++ 检测新线c++;fstream,c++,fstream,file-io,C++,Fstream,File Io,如何读取一个.txt文件?通过使用fstream将内容复制到另一个.txt文件中,以获得类似的内容。 问题是,当文件中有新行时。在使用ifstream时如何检测到这一点 用户输入“苹果” 例如: note.txt=> 我昨天买了一个苹果。 这苹果尝起来很好吃 注意_new.txt=> 我昨天买了一本书。 味道鲜美 结果注释假设在上面,但是: 注意_new.txt=> 我昨天买了一本书。味道鲜美 如何检查源文件中是否有新行,它还会在新文件中创建新行 这是我目前的代码: #include <i

如何读取一个.txt文件?通过使用fstream将内容复制到另一个.txt文件中,以获得类似的内容。 问题是,当文件中有新行时。在使用ifstream时如何检测到这一点

用户输入“苹果”

例如: note.txt=> 我昨天买了一个苹果。 这苹果尝起来很好吃

注意_new.txt=> 我昨天买了一本书。 味道鲜美

结果注释假设在上面,但是: 注意_new.txt=> 我昨天买了一本书。味道鲜美

如何检查源文件中是否有新行,它还会在新文件中创建新行

这是我目前的代码:

#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 << " ";
    }
}
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream INFLE(“note.txt”);
字符串字;
出流管(注:new.txt);
while(内嵌>>word){
逐行出孔法
如果仍要逐行执行,可以使用:


编辑:它将允许删除您不希望出现在新文件中的单词:

我们使用:

#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream INFLE(“note.txt”);
弦线;
字符串wordEntered(“apple”);//从命令行获取它
出流管(注:new.txt);
while(getline(infle,line)){
弦流ls(线);
字符串字;
while(ls>>word)
{
if(word!=wordEntered)
{

outFile有一种更简单的方法来完成这项工作:

#include <fstream>

int main() {
    std::ifstream inFile ("note.txt");
    std::ofstream outFile("note_new.txt");

    outFile << inFile.rdbuf();
}
#包括
int main(){
标准::ifstream infle(“note.txt”);
标准:流出管(“note_new.txt”);

如果要从输入文件中删除文本(如您的描述所示,但未说明),则输出文件

然后你需要逐行阅读,但是每一行都需要逐字解析,以确保你可以删除你正在寻找的工作
apple

#include <fstream>
#include <string> 

using namespace std;
// Don't do this. 

int main(int argc, char* argv[])
{
    if (argv == 1) { std::cerr << "Usage: Need a word to remove\n";exit(1);}
    std::string userWord = argv[1];  // Get user input (from command line)

    std::ifstream inFile("note.txt");
    std::ofstream outFile("note_new.txt");
    std::string   line;

    while(std::getline(inFile, line))
    {
         // Got a line
         std::stringstream linestream(line);
         std::string  word;

         while(linestream >> word)
         {
                // Got a word from the line.
                if (word != userWord)
                {
                     outFile << word;
                }
         }
         // After you have processed each line.
         // Add a new line to the output.
         outFile << "\n";
    }
}
#包括
#包括
使用名称空间std;
//不要这样做。
int main(int argc,char*argv[])
{
如果(argv==1){std::cerr>word)
{
//从电话里听到一句话。
if(word!=userWord)
{

outFile提示:
std::getline()
逐字解析文件有什么原因吗?获取相同文件的最简单方法是根本不解析,只读取/写入二进制数据,或者使用任何平台特定的api来复制文件。@RetiredInja实际上我也会检查检索到的字与用户指定的字是否相同,然后我就不写了新文件中的该词。因此,它将删除与用户指定的词相同的词。您应该在问题中指定该词,因为它不符合“完全相同的内容”部分。您可能希望在单词之间添加空格,最有可能是在每行第一个标记后写入每个标记之前,以防止在行尾出现尾随空格。即使这样,如果这很重要,它也不会在文件中保留任何额外的空格。@RetiredInja:的确如此。还需要补偿标点符号。但我不能做所有事情ng。
#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;
    }
}
#include <fstream>

int main() {
    std::ifstream inFile ("note.txt");
    std::ofstream outFile("note_new.txt");

    outFile << inFile.rdbuf();
}
#include <fstream>
#include <string> 

using namespace std;
// Don't do this. 

int main(int argc, char* argv[])
{
    if (argv == 1) { std::cerr << "Usage: Need a word to remove\n";exit(1);}
    std::string userWord = argv[1];  // Get user input (from command line)

    std::ifstream inFile("note.txt");
    std::ofstream outFile("note_new.txt");
    std::string   line;

    while(std::getline(inFile, line))
    {
         // Got a line
         std::stringstream linestream(line);
         std::string  word;

         while(linestream >> word)
         {
                // Got a word from the line.
                if (word != userWord)
                {
                     outFile << word;
                }
         }
         // After you have processed each line.
         // Add a new line to the output.
         outFile << "\n";
    }
}