0% found this document useful (0 votes)
31 views1 page

Лаба 4

The document contains a C++ program that reads lines from a file named 'file.txt', reverses the order of characters in each line, and then writes the modified lines to a new file called 'file2.txt'. It handles file opening errors and processes up to five lines of text, ensuring proper character manipulation. The program uses standard input/output streams and string handling functions.

Uploaded by

KAPPA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views1 page

Лаба 4

The document contains a C++ program that reads lines from a file named 'file.txt', reverses the order of characters in each line, and then writes the modified lines to a new file called 'file2.txt'. It handles file opening errors and processes up to five lines of text, ensuring proper character manipulation. The program uses standard input/output streams and string handling functions.

Uploaded by

KAPPA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

setlocale(LC_ALL, "rus");

const int s = 5, l = 100;


char ph[s][l];
int a = 0, b = 0;
const char enter = '\n';
ifstream fin("file.txt", ios::in);

if(!fin.is_open())
cout << "Файл не может быть открыт!\n";
else
{
for (int i = 0; i < s; i++) {
fin.getline(ph[i], l - 1, enter);
int x = strlen(ph[i]);
if (x != 0) {
for (int r = 0; r < x + 1; r++) {
if (ph[i][r] == ' ' || r == x) {
b = r - 1;
char w = ph[i][b];
ph[i][b] = ph[i][a];
ph[i][a] = w;
a = b + 2;
}
}
cout << ph[i] << endl;
a = 0, b = 0;
}
}
}
fin.close();

cout << "\n";

ofstream fout("file2.txt", ios::out);


for (int i = 0; i < s; i++) {
fout << ph[i] << endl;
}

return 0;

You might also like