Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
31
Добавлен:
17.04.2013
Размер:
1.94 Кб
Скачать
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>

void MakeFile(int n){                             	//zapolnyaem file randomom
   cout << "Making File out.txt" << endl;
	ofstream oFile("out.txt", ios::out);
   if(!oFile){
   	cerr << "Error occured!" << endl;
      exit(1);
   }
   randomize();
   for(int i=0; i<n; i++)
   	oFile << (random(50)-25) << " ";
   cout << "Done!" << endl;
}

void ShowFile(char * filename){                    		//pokaz file
   cout << "Showing File " << filename << endl;
   int i;
	ifstream iFile(filename, ios::in);
   if(!iFile){
   	cerr << "Error occured!" << endl;
      exit(1);
   }
   iFile >> i;
   while(!iFile.eof()){
   	cout << i << " ";
      iFile >> i;
   }
   cout << endl;
}

void Copy(){
   int f,l,n,i;

	ifstream iFile("out.txt", ios::in);
   if(!iFile){
   	cerr << "Error occured!" << endl;
      exit(1);
   }

   iFile >> f;
   n = 1;
   while(iFile){				//poschitaem kol-vo elementov
   	n++;
      iFile >> f;
	}
   iFile.close();


   ifstream iFile2("out.txt", ios::in);	//zapishem vse elementy v massiv

   int * a = new int[n];

   iFile2 >> f;
   i = 0;
   a[i] = f;

   while(iFile2){
      iFile2 >> f;
      i++;
      a[i] = f;
   }
   iFile2.close();

   ofstream oFile("file_g.txt", ios::out);
   if(!oFile){
   	cerr << "Error occured!" << endl;
   	exit(1);
   }

   ofstream oFile2("file_h.txt", ios::out);
   if(!oFile){
   	cerr << "Error occured!" << endl;
   	exit(1);
   }

   for(int j=0; j<n-1; j++)
   	if((a[j]%2==0 || a[j]%4==0) && a[j]%6!=0)
        	oFile << a[j] << " ";
      else
      	oFile2 << a[j] << " ";
}



int main(){
   int n;
   cout << "Enter number of elements : ";
   cin >> n;
   MakeFile(n);
   ShowFile("out.txt");
   Copy();
   ShowFile("file_g.txt");
   ShowFile("file_h.txt");
   getch();
	return 0;
}
Соседние файлы в папке file