Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
шпоры ппвис 2 часть с++.doc
Скачиваний:
26
Добавлен:
01.03.2016
Размер:
265.22 Кб
Скачать

64. Пример создания и заполнения файла.

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

int main()

{ char s[20];

char k[]="D:\\text.txt";

int i, kol;

ofstream Out;

Out.open(k);

if (!Out)

cout<<"ERROR";

for (i=0; i<10; i++)

{

cin>>s;

cin>>kol;

cout<<setw(20)<<left<<s;

cout<<setw(10)<<right<<kol<<endl;

Out<<setw(20)<<left<<s<<setw(10)<<right<<kol<<endl;

}

Out.close();

return 0;

}

65. Пример открытия и чтения содержимого файла.

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

int main()

{

ifstream In;

In.open(k);

if (!In)

cout<<"ERROR";

while (!In.eof())

{

In>>s>>kol;

cout<<setw(20)<<left<<s;

cout<<setw(10)<<right<<kol<<endl;

}

return 0;

}

66. Пример заполнения файла записями (значение-значение, размещенными в полях 20 и 30 позиций, с прижатием соответственно влево и вправо).

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

int main()

{ char s[20];

char k[]="D:\\text.txt";

int i, kol;

ofstream Out;

Out.open(k);

if (!Out)

cout<<"ERROR";

for (i=0; i<10; i++)

{

cin>>s;

cin>>kol;

cout<<setw(20)<<left<<s;

cout<<setw(10)<<right<<kol<<endl;

Out<<setw(20)<<left<<s<<setw(10)<<right<<kol<<endl;

}

Out.close();

return 0;

}