Homework: File typesetting (text file reading and writing)

[problem description]

The list of actors in English films is usually typeset and displayed in some way. Given an unformatted file listin Txt, the list of participants in each line of the file is separated into two parts by a colon "ldquo:rdquo", but the format is messy. Words (composed of characters other than spaces and horizontal tabs) may be separated by multiple spaces or horizontal tabs. Write a program and output it to another file listout according to the following layout rules Txt:

1. Input an integer from the standard as the fixed position of all lines of colons in one line after typesetting. The input integer must be greater than the number of characters before all lines of colons after typesetting, and the position is counted from 1;

2. The word string on the left of the colon is aligned to the left based on the line header, and the space between the last word on the left and the colon is filled in; The word string to the right of the colon is aligned to the left based on the colon. After the last word, there is only carriage return and line feed, and there are no other characters;

3. There is only one space between the words on the left and right sides of the colon, and at least one space on both sides of the colon is required.

It is assumed that the number of characters per line in the input file does not exceed 100.

[input form]

The list of participants to be typeset is from listin. In the current directory Txt file, the integer representing the colon position is read from standard input.

[output form]

The list of participants after typesetting is output to listout.com in the current directory Txt.

[input example]

Suppose the file listin Txt:

   Digital Intermediate by :   EFILM

Supervising    Digital Colorist : STEVEN J. SCOTT  

 Second Colorist :ANDREW FRANCIS

 Digital Intermediate Producer:LOAN PHAN

Digital  Intermediate Editor:  DEVON MILLER    

The integer indicating the fixed position of the colon is:

40

[output example]

File listout Txt should read:

[example description]

Input file listin Txt has a list of five lines of participants. It is required that the colon be located at the 40th character after typesetting and output to the file listout.txt according to the above typesetting rules Txt.

resolvent

This problem mainly focuses on two operations: reading and writing text files and string processing.

The file reading and writing of this problem is just a routine operation. What is a little difficult is string processing.

According to the requirements of the topic, we process this line of string for each line read from the text.

The headache is that there may be continuous spaces and line breaks in the middle of each line of characters in the inlist file, which are not required in the outlist. How to clear these continuous spaces and line breaks is a problem we should consider carefully.

Let's find the position of ":" and divide a line into two substrings for processing. Read the characters in sequence. If it is a newline character, change it to a space (it can't be completely deleted because a space should be reserved between words); If it is neither a colon nor a newline character, the substring is read in. There are several situations that will make the substring have redundant spaces after processing: space + space; Space + newline; Newline + space; Newline + newline. So we put these four situations at the forefront.

After this processing, the two substrings may still retain redundant spaces, but there is no newline.

Where are these extra spaces?

At the beginning of the substring.

So we just need to clean up the opening space. Use the iterator to point to the beginning of the substring. If it is a space, delete it. If not, exit the loop. Note that there is no need to move the position of the iterator in the loop, because the first space is deleted. After each deletion, the iterator is still at the beginning.

(the function of the string class is very powerful. Make good use of it and you can easily handle strings.)

#include <iostream>
#include<fstream>
#include<iomanip>
using namespace std;

int main()
{
    int n;
    cin>>n;
    ifstream infile("listin.txt",ios::in);
    ofstream outfile("listout.txt",ios::out);
    string temp;
    if(!infile.is_open())
    {
        cerr<<"Failed!"<<endl;
        exit(1);
    }
    while(getline(infile,temp))
    {

        string first,second;
        unsigned index=0;
        for(unsigned i=0; i<temp.size(); ++i)
        {
            if((temp[i]==' '&&temp[i+1]==' ')||(temp[i]=='\t'&&temp[i+1]==' ')||(temp[i]==' '&&temp[i+1]=='\t') ||(temp[i]=='\t'&&temp[i+1]=='\t'))continue;
            else if(temp[i]!=':'&&temp[i]!='\t')
                first+=temp[i];
            else if(temp[i]=='\t')
                first+=' ';
            else if(temp[i]==':')
            {
                index=i;
                break;
            }
        }


            while(1)
            {
                string::iterator p=first.begin();
                if(*p==' ')
                first.erase(p);
                else break;
            }


            for(unsigned i=index+1; i<temp.size(); ++i)
            {

                if((temp[i]==' '&&temp[i+1]==' ')||temp[i]=='\t')continue;
                else second+=temp[i];
            }

            while(1)
            {string::iterator q=second.begin();
            if(*q==' ')
                second.erase(q);
            else break;
            }
            outfile<<setw(n-1)<<left<<first<<": "<<left<<second<<endl;

        }

    infile.close();
    outfile.close();
    return 0;
}

When I was writing this question, it took me a long time to clear the spaces. In particular, I didn't understand why two consecutive spaces would be retained after processing. Finally, I thought of four cases. In addition, it will be more troublesome to clean up the beginning spaces without iterators and erase functions.

Keywords: C++ string

Added by cleromancer on Mon, 03 Jan 2022 07:54:37 +0200