简体   繁体   中英

reading multiple lines from .txt file as string removing white spaces and creating new file for output

I am trying to write a program that reads information from a .txt file, removes the unneeded spaces between the words/parts and saves the outcome to a new output .txt file.

I have managed to get most of this working after looking through a lot of questions on the site for some guidance. Currently I have the code reading from the .txt file and writing to a new one, I have also managed to get it to remove the unneeded spaces. However now that I have managed to get this part running it only will read one line from the original .txt file and stops there. It is also now writing to the output file every version of the line it grabs removing each space.

Here is the code I have managed to write so far, any advice on any part would be grateful as I am still learning.

#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;
}

Here is a sample of the input data:

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 

Here is a small sample of the output to show what is now happening:

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               

Where possible I want to have the line without spaces put into the output on its own without seeing all the working. Then the same for the next line from the original input which currently is not being processed. And this would have to run for however many lines the original .txt file has which can change each time.

You're calling OriginalInputFile.close() after writing each line to the output file. So, only the first line of the input file is ever read before the file is closed (after which, getline(OriginalInputFile,Inputfile) will no linger be true, so the top while loop exits after a single iteration.

Here's an untested edit that should hopefully work:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM