 Четене и
запис в текстови файлове
 Четене и
запис в текстови файлове
Б) файл за писане:
- дефиниране на файлова променлива за писане
   ofstream out_data;
- отваряне на файл
   out_data.open("output.dat");
- писане във  файл
--- с изходен поток:
   int n = 2;
   double x = 1.5;
   string s = " Hello";
   out_data << n <<
"  " << x << s;
--- с член-функции
   char ch = 'A';
   out_data.put(ch);
- затваряне на файл
   out_data.close();
В) файл за четене и писане:
- дефиниране на файлове променлива за четене и писане
   fstream io_data;
- отваряне на файл
   io_data.open("data.dat");
* Програма за четене на заплати и чертаене на диаграма
// readdata.cpp
#include "ccc_win.cpp"
void read_data(vector<double>& v,
ifstream& in)
{
   double s;
   while (in >> s) v.push_back(s);
}
double maximum(vector<double> v)
{
   if (v.size() == 0) return
0;
   double highest = v[0];
   int i;
   for (i = 1; i < v.size();
i++)
      if (v[i]
> highest) highest = v[i];
   return highest;
}
void plot_data(vector<double> v)
{
   double highest = maximum(v);
   cwin.coord(0, 0, highest,
v.size());
   int i;
   for (i = 0; i < v.size();
i++)
   {  Point left(0, i);
      Point right(v[i],
i);
      Line bar(left,
right);
      Message
label(left, v[i]);
      cwin <<
bar << label;
   }
}
int main()
{  string filename
      = cwin.get_string("Please
enter the data file name: ");
   ifstream infile;
   infile.open(filename.c_str());
   vector<double> data;
   read_data(data, infile);
   infile.close();
   plot_data(data);
   return 0;
}
 Текстови потоци
 Текстови потоци
* Входен поток
#include <sstream>
...
string input = "January 23, 1881";
istringstream instr(input);
string month;
int day;
string comma;
int year;
instr >> month >> day >> comma >> year;
* Изходен поток
ostringstream outstr;
outstr << setprecision(8) <<
sqrt(2);
string output = outstr.str();
// readtime.cpp
#include <iostream>
#include <sstream>
using namespace std;
string int_to_string(int n)
{
   ostringstream outstr;
   outstr << n;
   return outstr.str();
}
void read_time(int& hours, int&
minutes)
{
   string line;
   getline(cin, line);
   istringstream instr(line);
   instr >> hours;
   minutes = 0;
   char ch;
   instr.get(ch);
   if (ch == ':') instr >> minutes;
   else
#ifndef CCC_USE_OLD_STREAMS
      instr.unget();
#else
      instr.putback(ch);
/* used if your compiler doesn't support
the ANSI unget function */
#endif
   string suffix;
   instr >> suffix;
   if (suffix == "pm") hours
= hours + 12;
}
string time_to_string(int hours, int minutes,
bool am_pm)
{
   string suffix;
   if (am_pm)
   {  if (hours < 12)
suffix = "am";
      else 
{ suffix = "pm"; hours = hours - 12; }
      if (hours
== 0) hours = 12;
   }
   string result = int_to_string(hours)
+ ":";
   if (minutes < 10) result
= result + "0";
   result = result + int_to_string(minutes);
   if (am_pm) result = result
+ " " + suffix;
   return result;
}
int main()
{  cout << "Please enter the
time: ";
   int hours;
   int minutes;
   read_time(hours, minutes);
   cout << "Using am/pm:  
" << time_to_string(hours, minutes, true) << "\n";
   cout << "Military time:
" << time_to_string(hours, minutes, false) << "\n";
   return 0;
}
 Аргументи
от командния ред
 Аргументи
от командния ред
* Определение и използване
C:/mydir/>prog -v input.dat
int main(int argc, char* argv[])
string(argv[0])    "prog"
string(argv[1])    "-v"
string(argv[2])    "input.dat"
* Шифроване на Цезар
// ceasar.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void usage(string program_name)
{
   cout << "Usage: " <<
program_name
       
<< " [-d] [-kn] infile outfile\n";
   exit(1);
}
void open_file_error(string filename)
{
   cout << "Error opening
file " << filename << "\n";
   exit(1);
}
int remainder(int a, int n)
{
   if (a >= 0) return a % n;
   else       
return n - 1 - (-a - 1) % n;
}
char encrypt(char ch, int k)
{
   const int NLETTER = 26;
   if ('A' <= ch &&
ch <= 'Z')
      return
static_cast<char>('A' + remainder(ch-'A'+k, NLETTER));
   if ('a' <= ch &&
ch <= 'z')
      return
static_cast<char>('a' + remainder(ch-'a'+k, NLETTER));
   return ch;
}
void encrypt_file(ifstream& in, ofstream&
out, int k)
{
   char ch;
   while (in.get(ch)) out.put(encrypt(ch,
k));
}
int string_to_int(string s)
{
   istringstream instr(s);
   int n;
   instr >> n;
   return n;
}
int main(int argc, char* argv[])
{  bool decrypt = false;
   int key = 3;
   int nfile = 0; /* the number
of files specified */
   ifstream infile;
   ofstream outfile;
if (argc < 3 or argc > 5) usage(string(argv[0]));
   int i;
   for (i = 1; i < argc;
i++)
   {  string arg = string(argv[i]);
      if (arg.length()
>= 2 and arg[0] == '-')
      /* it is
a command line option */
      { 
char option = arg[1];
        
if (option == 'd')
        
decrypt = true;
        
else if (option == 'k')
        
key = string_to_int(arg.substr(2, arg.length() - 2));
      }
      else
      { 
nfile++;
        
if (nfile == 1)
        
{  infile.open(arg.c_str());
           
if (infile.fail()) open_file_error(arg);
        
}
        
else if (nfile == 2)
        
{  outfile.open(arg.c_str());
           
if (outfile.fail()) open_file_error(arg);
        
}
      }
   }
if(nfile != 2) usage(string(argv[0]));
if (decrypt) key = -key;
   encrypt_file(infile, outfile,
key);
   infile.close();
   outfile.close();
   return 0;
}
 Произволен
достъп
 Произволен
достъп
* Специфични функции
fs.seekg(n, ios::beg);
fs.seekp(n, ios::beg);
fs.tellg();
fs.tellp();
* Пример с база-данни за повишаване на заплати
// database.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
#include "ccc_empl.cpp"
const int NEWLINE_LENGTH = 2; /* or 1 on
Unix */
const int RECORD_SIZE = 30 + 10 + NEWLINE_LENGTH;
double string_to_double(string s)
{
   istringstream instr(s);
   double x;
   instr >> x;
   return x;
}
void raise_salary(Employee& e, double
percent)
{
   double new_salary = e.get_salary()
* (1 + percent / 100);
   e.set_salary(new_salary);
}
void read_employee(Employee& e, fstream&
fs)
{
   string line;
   getline(fs, line);
   if (fs.fail()) return;
   string name = line.substr(0,
30);
   double salary = string_to_double(line.substr(30,
10));
   e = Employee(name, salary);
}
void write_employee(Employee e, fstream&
fs)
{
   fs << e.get_name()
      <<
setw(10 + (30 - e.get_name().length()))
      <<
fixed << setprecision(2)
      <<
e.get_salary()
      <<
"\n";
}
int main()
{  cout << "Please enter the
data file name: ";
   string filename;
   cin >> filename;
   fstream fs;
   fs.open(filename.c_str());
   fs.seekg(0, ios::end); /*
go to end of file */
   int nrecord = fs.tellg()
/ RECORD_SIZE;
   cout << "Please enter
the record to update: (0 - "
      <<
nrecord - 1 << ") ";
   int pos;
   cin >> pos;
const double SALARY_CHANGE = 5.0;
   Employee e;
   fs.seekg(pos * RECORD_SIZE,
ios::beg);
   read_employee(e, fs);
   raise_salary(e, SALARY_CHANGE);
   fs.seekp(pos * RECORD_SIZE,
ios::beg);
   write_employee(e, fs);
   fs.close();
   return 0;
}