using namespace std; void ProbelVoid(char *str) { char *str1=new char [512]; int i = 0; for (char* a = str, *b = str1; *a; ++a, ++b, i++) { for (; *a == ' ' && *(a + 1) == ' '; a++); *b = *a; } str1[i] = '\0'; strcpy_s(str, strlen(str1), str1); } int main() { ifstream in("1.txt"); if (!in) { cout << "Ошибка: не могу открыть входной файл "; system("pause"); return-1; } char *str=new char[512]; in.getline(str, 512); cout << "Изначальная строка\n" << str; ProbelVoid(str); cout << "\n\nИзменённая строка\n" << str; ofstream out("output.txt"); out << str; cout << endl; out.close(); in.close(); system("pause"); return 0; } 

I can not alter under a dynamic array, so that from the file without problems you can use any size of text in the program (removes extra spaces)

  • one
    Do you need to write on si? Why not use std::string ? (that memory needs to be freed, I am already silent) - KoVadim
  • I don’t know how to determine the length of the source string from the file to create a dynamic array and pass it to the function, I know about deletion, I just haven’t reached yet - Matvey Baygin
  • one
    just read line by line from a file in std::string using std::getline . All machine will be highlighted. - KoVadim

2 answers 2

In C ++, there is already a class that encapsulates a dynamic array of characters. This is the class std::string .

You can use the standard function std::getline to read from a stream of lines.

Also in C ++ there is a standard algorithm std::unique which allows you to exclude duplicate characters from a string.

Below is a demo program that shows how to use these tools. In this program, instead of a file, a string stream is used for simplicity, and the output is also carried out not to a file, but to the console. It is not difficult to modify the program by replacing the string stream and the console with input and output files.

 #include <iostream> #include <algorithm> #include <sstream> int main() { std::istringstream is( "Hello, World!\nI am learning C++" ); std::string s; while ( std::getline( is, s ) && !s.empty() ) { auto it = std::unique( s.begin(), s.end(), []( char c1, char c2 ) { return ( c1 == ' ' || c1 == '\t' ) && ( c1 == c2 ); }); s.erase( it, s.end() ); std::cout << s << std::endl; } return 0; } 

Console output

 Hello, World! I am learning C++ 

As can be seen from the output, duplicate space characters are excluded from the input string.

If the input file can contain empty lines, then the second condition from the while loop should be removed. For example,

 while ( std::getline( is, s ) ) 

But you can include this condition to write the resulting lines to the output file, that is, you can not write empty lines to the output file. For example,

  if ( !s.empty() ) std::cout << s << std::endl; 

    And why do not you solve your problem easier and shorter?

     int main(int argc, const char * argv[]) { char cur, last = 0; while(cin.get(cur)) { if (isspace(cur) && isspace(last)) continue; cout << (last = cur); } } 

    Duplicate whitespace characters are thrown from the entire input:

     qwtdcas jahsghj hbdjhbhjd dyuygd yeduy tyuyd qwtdcas jahsghj hbdjhbhjd dyuygd yeduy tyuyd ^Z 

    The length of the string is completely unimportant :)

    If you need to restrict one line - after

      cout << (last = cur); 

    add

      if (cur == '\n') break;