Специалист
Регистрация: 27.08.2008
Адрес: Санкт-Петербург
Сообщений: 27,807
Сказал(а) спасибо: 340
Поблагодарили 583 раз(а) в 208 сообщениях
Репутация: 113184
|
Цитата:
Сообщение от exel
имеется текстовый файл из нескольких строк (скажем, не более 100 символов в строке). организовать вывод строк в другой файл в обратном порядке. Использовать память экономно - в качестве буфера в программе применить строку. ее длина должна быть заведомо меньше длины исходного файла. помогите плиз
|
Легко.
Например, так (имена и адреса файлов поставьте, конечно, свои):
Код:
Var
f1,f2:Text;
i,N:byte;
S:String;
Begin
Assign(f1,'D:\in.txt');
Reset(f1);
Assign(f2,'D:\out.txt');
Rewrite(f2);
N:=1;
Repeat
Readln(f1,S);
if Not(EoF(f1)) then Inc(N);
Until EoF(f1);
Repeat
Reset(f1);
for i:=1 to N do Readln(f1,S);
Writeln(f2,S);
Dec(N);
Until N=0;
Close(f1);
Close(f2);
End.
Результат:
файл in.txt:
Friends, Romans, countrymen, lend me your ears;
I come to bury Caesar, not to praise him.
The evil that men do lives after them;
The good is oft interred with their bones;
So let it be with Caesar. The noble Brutus
Hath told you Caesar was ambitious:
If it were so, it was a grievous fault,
And grievously hath Caesar answer'd it.
Here, under leave of Brutus and the rest -
For Brutus is an honourable man;
So are they all, all honourable men -
Come I to speak in Caesar's funeral.
He was my friend, faithful and just to me:
But Brutus says he was ambitious;
And Brutus is an honourable man.
He hath brought many captives home to Rome
Whose ransoms did the general coffers fill:
Did this in Caesar seem ambitious?
When that the poor have cried, Caesar hath wept:
Ambition should be made of sterner stuff:
Yet Brutus says he was ambitious;
And Brutus is an honourable man.
You all did see that on the Lupercal
I thrice presented him a kingly crown,
Which he did thrice refuse: was this ambition?
Yet Brutus says he was ambitious;
And, sure, he is an honourable man.
I speak not to disprove what Brutus spoke,
But here I am to speak what I do know.
You all did love him once, not without cause:
What cause withholds you then, to mourn for him?
O judgment! thou art fled to brutish beasts,
And men have lost their reason. Bear with me;
My heart is in the coffin there with Caesar,
And I must pause till it come back to me.
файл out.txt:
And I must pause till it come back to me.
My heart is in the coffin there with Caesar,
And men have lost their reason. Bear with me;
O judgment! thou art fled to brutish beasts,
What cause withholds you then, to mourn for him?
You all did love him once, not without cause:
But here I am to speak what I do know.
I speak not to disprove what Brutus spoke,
And, sure, he is an honourable man.
Yet Brutus says he was ambitious;
Which he did thrice refuse: was this ambition?
I thrice presented him a kingly crown,
You all did see that on the Lupercal
And Brutus is an honourable man.
Yet Brutus says he was ambitious;
Ambition should be made of sterner stuff:
When that the poor have cried, Caesar hath wept:
Did this in Caesar seem ambitious?
Whose ransoms did the general coffers fill:
He hath brought many captives home to Rome
And Brutus is an honourable man.
But Brutus says he was ambitious;
He was my friend, faithful and just to me:
Come I to speak in Caesar's funeral.
So are they all, all honourable men -
For Brutus is an honourable man;
Here, under leave of Brutus and the rest -
And grievously hath Caesar answer'd it.
If it were so, it was a grievous fault,
Hath told you Caesar was ambitious:
So let it be with Caesar. The noble Brutus
The good is oft interred with their bones;
The evil that men do lives after them;
I come to bury Caesar, not to praise him.
Friends, Romans, countrymen, lend me your ears;
|