【问题标题】:reading multiple lines from .txt file as string removing white spaces and creating new file for output从 .txt 文件中读取多行作为字符串删除空格并为输出创建新文件
【发布时间】:2018-11-21 22:34:41
【问题描述】:

我正在尝试编写一个从 .txt 文件中读取信息的程序, 删除单词/部分之间不需要的空格并将结果保存到新的输出 .txt 文件中。

在查看了网站上的许多问题以获得一些指导后,我已经设法完成了大部分工作。目前我从 .txt 文件中读取代码并写入新文件,我还设法让它删除不需要的空格。但是,既然我已经设法让这部分运行,它只会从原始 .txt 文件中读取一行并停在那里。它现在也在将它抓取的每个版本的行的每个版本都写入输出文件,删除每个空格。

这是我迄今为止编写的代码,任何部分的任何建议都将不胜感激,因为我仍在学习。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Declarations
string Inputfile, Outputfile;
ifstream OriginalInputFile("Sample of input file.txt");
ofstream NewOutputFile("Output File.txt");
ifstream OutputFileRead("Output File.txt");

int main()
{
    if (!OriginalInputFile.is_open()) { 
        cout << "Input file could not be opened! Terminating!" << endl;
        return 1;
    }
    if (OriginalInputFile.is_open())
    {
        NewOutputFile << "                         EXCEPTIONS REPORT                    " << endl;
        NewOutputFile << "                       PP TO FS OO INTERFACE                  " << endl;
        NewOutputFile << "     =========================================================" << endl;

        while ( getline (OriginalInputFile,Inputfile) )
            while(true)
            {
                unsigned int pos = Inputfile.find("  ");
                if(pos == string::npos)
                {
                    break;
                }
                else 
                {
                    Inputfile.erase(pos, 1);
                }
                {
                    Outputfile = Inputfile;
                    NewOutputFile << Outputfile << endl;
                    OriginalInputFile.close();
                }
            }
    }

    if (!NewOutputFile.is_open()) { 
        cout << "Output file could not be opened! Terminating!" << endl;
        return 1;
    }
    if (NewOutputFile.is_open()) 
    {
        while ( getline (OutputFileRead, Outputfile))
        {
            cout << Outputfile << endl;
        }
        {
            NewOutputFile.close();
        }
    }
    return 0;
}

这是输入数据的示例:

BABROUB00008         PERSON1             MARTIN                        M               0610196129081978D B09          PM           Brough         B010           B00008    sfsf.testmailbox@mysite.com                                       54289                                                      
BABROUB00012         PERSON2             TIMOTHY                       T               1708196407091981D B08          PP           Brough         B306           B00012    sfsf.testmailbox@mysite.com   

                                53899 

这里是输出的一个小样本,以显示现在正在发生的事情:

BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com          54289                                                      
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com         54289                                                      
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com        54289                                                      
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com       54289                                                      
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com      54289                                                      
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com     54289                                                      
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com    54289                                                      
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com   54289                                                      
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com  54289                                                      
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289               

在可能的情况下,我希望将没有空格的行单独放入输出中,而不会看到所有的工作。然后对于当前未处理的原始输入的下一行也是如此。无论原始 .txt 文件有多少行,这都必须运行,每次都可以更改。

【问题讨论】:

  • 您正在将中间步骤写入输出文件。如果您正确缩进源文件,您将看到写入输出在 while(true) 循环内。
  • 谢谢,你能给我举个例子来说明你的意思吗,因为我不太确定刚接触这门语言的人到底是什么意思。
  • 玩了一会儿后,我成功地做到了你所说的谢谢你,它奏效了。但是,它仍然只是从原始输入文件中读取第一行,您知道我怎样才能让它读取原始输入文件中的所有行吗?我确定这是我缺少的一些简单的东西。再次感谢您

标签: c++ string ifstream ofstream


【解决方案1】:

在将每一行写入输出文件后,您正在调用OriginalInputFile.close()。因此,在文件关闭之前只读取输入文件的第一行(此后,getline(OriginalInputFile,Inputfile) 将不再为真,因此顶部的while 循环在单次迭代后退出。

这是一个未经测试的编辑,希望可以工作:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Declarations
string Inputfile, Outputfile;
ifstream OriginalInputFile("Sample of input file.txt");
ofstream NewOutputFile("Output File.txt");
ifstream OutputFileRead("Output File.txt");

int main()
{
    if (!OriginalInputFile.is_open()) { 
        cout << "Input file could not be opened! Terminating!" << endl;
        return 1;
    }
    if (!NewOutputFile.is_open()) { 
        cout << "Output file could not be opened! Terminating!" << endl;
        return 1;
    }

    NewOutputFile << "                         EXCEPTIONS REPORT                    " << endl;
    NewOutputFile << "                       PP TO FS OO INTERFACE                  " << endl;
    NewOutputFile << "     =========================================================" << endl;

    while ( getline (OriginalInputFile,Inputfile) )
    {
        for(unsigned int pos=Inputfile.find("  "); pos!=string::npos; pos=Inputfile.find("  "))
        {
            Inputfile.erase(pos, 1);
        }
        Outputfile = Inputfile;
        NewOutputFile << Outputfile << endl;
    }
    OriginalInputFile.close();

    while ( getline (OutputFileRead, Outputfile))
    {
        cout << Outputfile << endl;
    }
    NewOutputFile.close();
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多