#include
#include
#include
const SIZEN = 25;
const SIZES = 81;
void main()
{char fname[SIZEN],str[SIZES];
cout << "File name: ";
cin.getline(fname, SIZEN-1);
// отваря файла за извличане и за вмъкване
fstream file(fname, ios::in|ios::out);
// проверка дали операцията е успешна
if(!file)
{cerr << "Not possible to use the file!\n";
return;
}
// Отварянето е успешно.
// Извлича първия низ.
file.getline(str, SIZES);
// Извежда го върху екрана.
cout << str << endl;
// въвежда от клавиатурата низ.
cout << "> "; cin.getline(str, SIZES);
// вмъква го във файла
file << str << endl;
}
Задача 2. Да се напише програма, която създава файла client.dat със следните данни за служител на фирма:
int account; (сметка)
char name[35]; (име)
float balance; (сума)
#include
#include
#include
void main()
{ofstream file(“clients.dat”, ios::out);
if(!file)
{cerr << "File coudn't be opened! \n";
return;
}
cout << "Enter the account, name and balance. \n"
<< "Enter enf_of_file to end input. \n?";
int account;
char name[35];
float balance;
while(cin >> account >> name >> balance)
{file << account << " " << name << " " << balance << '\n';
cout << '?';
}
file.close();
}
Задача 3. Да се напише програма, която добавя записи от вида:
int account; (сметка)
char name[35]; (име)
float balance; (сума)
в края на файла clients.dat.
Налага се само следната промяна в решението на Задача 2:
ofstream file(“clients.dat”, ios::out);
да се замени с:
ofstream file(“clients.dat”, ios::app);
или с
ofstream file(“clients.dat”, ios::ate);
Задача 4. Да се напише програма, която многократно сканира файла clients.dat и дава информация за служителите със заплата 0 (balance е 0), със заплата на кредит (balance е отрицателно) или със заплата на дебит (balance е положително).
Изборът на вида на справката се управлява чрез изброимия тип ReqType:
enum ReqType{ZEROBALANCE = 1, CREDITBALANCE, DEBITBALANCE, END};
#include
#include
#include
enum ReqType{ZEROBALANCE = 1, CREDITBALANCE,
DEBITBALANCE, END};
int getRequest()
{int request;
do { cout << endl; cin >> request;
} while(request < ZEROBALANCE || request > END);
return request;
}
bool shouldDisplay(int type, double balance)
{if (type == CREDITBALANCE && balance < 0) return true;
if (type == DEBITBALANCE && balance > 0) return true;
if (type == ZEROBALANCE && balance == 0) return true;
return false;
}
void OutLine(int acc, const char *name, double bal)
{cout << setiosflags(ios::left) << setw(10) << acc
<< setw(13) << name << setw(7) << setprecision(2)
<< resetiosflags(ios::left)
<< setiosflags(ios::fixed|ios::showpoint)
<< bal << endl;
}
void main()
{ifstream file("clients.dat", ios::in);
if(!file)
{cerr << "File coudn't be opened! \n";
return;
}
int request; int account; char name[35];
float balance;
cout << "Enter request\n"
<< "1 - List accounts with zero balanses\n"
<< "2 - List accounts with credit balanses\n"
<< "3 - List accounts with debit balanses\n"
<< "4 - End of run";
request = getRequest();
while (request != END)
{switch (request)
{case ZEROBALANCE : cout << "\nAccounts with zero balances\n";
break;
case CREDITBALANCE :cout << "\nAccounts with credit balances\n";
break;
case DEBITBALANCE : cout << "\nAccounts with debit balances\n";
break;
}
file >> account >> name >> balance;
while (!file.eof())
{if (shouldDisplay(request, balance))
OutLine(account, name, balance);
file >> account >> name >> balance;
}
file.clear();
file.seekg(0);
request = getRequest();
}
cout << "End of run. " << endl;
}
Сподели с приятели: