Home c++ Working with files. C++

Working with files. C++

Author

Date

Category

k (& gt; 25) is an integer, and the compressed text file is given below

text in the problem.txt file:
___ Object-oriented programming (OOP) – a programming methodology based on the program presentation in the form of a set of objects, each of which is a copy of a certain class, and classes form the inheritance hierarchy.
___ ideologically OOP – approach to programming as a modeling of information objects, decisive at the new level the main problem of structural programming: structuring information from the point of view of controllability, which significantly improves the controllability of the modeling process itself, which, in turn, is especially important when implementing large projects.

Here ___ When a space, the designation of the red row

Paragraph is separated by a red string, no empty rows. Format the text like this
So that its width does not exceed K characters and was aligned on the left edge, keeping the gaps of paragraph.
Remove the spaces at the end of the line. Save the formatted text in a new file.

What I was able to do (

# include & lt; fstream & gt;
Using Namespace STD;
INT MAIN () {
  SETLOCALE (LC_ALL, "RUS");
  int k;
  cout & lt; & lt; "ENTER K (K & GT; 25) =";
  CIN & GT; & gt; k;
  if (k & lt; = 25) {
    COUT & LT; & LT; "Error, R & LT; = 25";
    EXIT (1);
  }
  ifstream reed;
  reed.open ("problem.txt");
  if (! reed.is_open ())
  {
    COUT & LT; & LT; "PROBLEM.TXT - FILE NOT FOUND \ N";
    EXIT (1);
  }
  String Get_String, Text;
  While (! Reed.eof ())
  {
    GetLine (Reed, Get_String);
    TEXT + = get_string;
  }
  Reed.Close ();
  Ofstream WRT ("main.txt");
  WRT & LT; & lt; text;
  Wrt.Close ();
  Return 0;
}

I do not understand how to implement, there is an idea that it is prettier to read text from the file, if they came to position K, then if it is empty, i.e. Space, replace \ N , if not, we go back and looking for a space. But I can not implement (
Help please


Answer 1, Authority 100%

If I understood everything correctly, then here is the code:

# include & lt; iostream & gt;
#Include & lt; fstream & gt;
#Include & lt; String & GT;
Using Namespace STD;
INT MAIN ()
{
  SETLOCALE (LC_ALL, "RUSSIAN");
  int k;
  COUT & LT; & LT; "Enter K (K & GT; 25):";
  CIN & GT; & GT; k;
  if (k & lt; = 25) {
    COUT & LT; & LT; "Error, R & LT; = 25!";
    EXIT (1);
  }
  Wifstream Input ("Problem.txt", iOS :: IN); // Wide Char version ifstream
  WOFSTREAM OUTPUT ("Main.txt", iOS :: OUT); // Wide Char Version Ofstream
  input.imbue (Locale ("ru_ru.utf8")); // Set the encoding for the input, problem.txt should also be in UTF8
  Output.imbue (Locale ("RU_RU.UTF8")); // Set the encoding for Output
  if (! input.is_open ()) {
    COUT & LT; & LT; "PROBLEM.TXT - File not found! \ N";
    EXIT (1);
  }
  WString Word; // Wide Char String Version
  INT COUNT = 0;
  While (Input & GT; & GT; Word)
  {
    BOOL ISLINEBIGGERTHANK = (Count + Word.Length ()) & gt; k;
    BOOL ISPARAGRAPH = WORD [0] == '_';
    if (islinebiggerthank || isparagraph) {
      Count = 0;
      Output & lt; & lt; '\ n';
    }
    Output & lt; & lt; Word & lt; & lt; '';
    Count + = Word.Length () + 1;
  }
  input.close ();
  Output.Close ();
  Return 0;
}

Submitted text to enter:

___ object-oriented programming (OOP) - a programming methodology based on the presentation of the program in the form of a set of objects, each of which is A copy of a certain class, and classes form a hierarchy of inheritance. ___ Ideologically OOP - approach to programming as a modeling of information objects, decisive at the new level the basic problem of structural programming: structuring information from the point of view of controllability, which significantly improves the controllability of the modeling process itself, which, in turn, is especially important when implementing large projects.

We get at the output (at k = 35):


___ Object-oriented
Programming (OOP) -
programming methodology,
Based on the presentation
Programs in the form of a set
objects each of which
It is an instance of definite
class and classes form a hierarchy
inheritance.
___ ideologically OOP - approach to
Programming as K.
Modeling information
Objects decisive at the new level
The main task of structural
Programming: Structuring
information from the point of view
manageability, which is essential
Improves the handling of themselves
process modeling that in
turn is especially important when
Implementation of large projects.

words that do not fit into the limit strings are transferred. You can of course make a split word and transfer, but this will complicate the code. Yes, and not sure what it is necessary.

UPD:
The code has updated, the previous version incorrectly considered the length of the word because of the wrong encoding.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions