void concatanate_index(char const * filename, char tmp[100], size_t index)
{
	 strcpy(tmp, filename);
  strcat(tmp, std::to_string(index).c_str());
}


int read_final_correction_word(bool party, DB_t& FCW_read)
{
  if(!party)
		{
   char tmp[100];
   concatanate_index("preprocflags/FCW0", tmp, 0);
			int const in0 { open(tmp, O_RDONLY ) };
	 	size_t r = read(in0, &FCW_read,   sizeof(FCW_read));	
   if(r < 0) perror("Read error");
		}
		
  if(party)
		{
   char tmp[100];
   concatanate_index("preprocflags/FCW1", tmp, 0);
		 int const in0 { open(tmp, O_RDONLY ) };
	 	size_t r = read(in0, &FCW_read,   sizeof(FCW_read));	
 		if(r < 0) perror("Read error");
		}

  return 0;
}

int read_flags_for_reading(bool party, size_t db_nitems)
{
	if(!party)
	{
   char tmp[100];
   concatanate_index("preprocflags/party0_read_flags_b", tmp, 0);
	 	int const in0 { open(tmp, O_RDONLY ) };
	 	size_t r = read(in0, reading_b,  db_nitems * sizeof(reading_b[0]));	
   if(r < 0) perror("Read error");
  
   concatanate_index("preprocflags/party0_read_flags_c", tmp, 0);
   int const in1 { open( tmp, O_RDONLY ) };
	 	r = read(in1, reading_c,  db_nitems * sizeof(reading_c[0]));
   if(r < 0) perror("Read error");

   concatanate_index("preprocflags/party0_read_flags_d", tmp, 0);
	  int const in2 { open( tmp, O_RDONLY ) };
	  r = read(in2, reading_d,  db_nitems * sizeof(reading_d[0]));	
   if(r < 0) perror("Read error");
	}

	if(party)
	{
   char tmp[100];
  
   concatanate_index("preprocflags/party1_read_flags_b", tmp, 0);
		 int const in0 { open(tmp, O_RDONLY ) };
	 	size_t r = read(in0, reading_b,  db_nitems * sizeof(reading_b[0]));	
	 	if(r < 0) perror("Read error");

   concatanate_index("preprocflags/party1_read_flags_c", tmp, 0);
 	 int const in1 { open(tmp, O_RDONLY ) };
		 r = read(in1, reading_c,  db_nitems * sizeof(reading_c[0]));
		 if(r < 0) perror("Read error");
    
   concatanate_index("preprocflags/party1_read_flags_d", tmp, 0);
		 int const in2 { open(tmp, O_RDONLY ) };
		 r = read(in2, reading_d,  db_nitems * sizeof(reading_d[0]));	
   if(r < 0) perror("Read error");
}

 
	return 0;	
}

int read_flags_for_writing(bool party, size_t db_nitems)
{
	if(!party)
	{
    char tmp[100];
    concatanate_index("preprocflags/party0_write_flags_b", tmp, 0);
	   int const in0_w { open(tmp, O_RDONLY ) };
    size_t r = read(in0_w, writing_b,  db_nitems * sizeof(writing_b[0]));	
    if(r < 0) perror("Read error");

    concatanate_index("preprocflags/party0_write_flags_c", tmp, 0);	   
	   int const in1_w { open( tmp, O_RDONLY ) };
    r = read(in1_w, writing_c,  db_nitems * sizeof(writing_c[0]));
    if(r < 0) perror("Read error");
	
    concatanate_index("preprocflags/party0_write_flags_d", tmp, 0);
	   int const in2_w { open( tmp, O_RDONLY ) };
   	r = read(in2_w, writing_d,  db_nitems * sizeof(writing_d[0]));	
    if(r < 0) perror("Read error");
	}

	if(party)
	{
   char tmp[100];
   concatanate_index("preprocflags/party1_write_flags_b", tmp, 0);
 		int const in0_w { open( tmp, O_RDONLY ) };
	 	size_t r = read(in0_w, writing_b,  db_nitems * sizeof(writing_b[0]));	
	 	if(r < 0) perror("Read error");

   concatanate_index("preprocflags/party1_write_flags_c", tmp, 0);
   int const in1_w { open(tmp, O_RDONLY ) };
		 r = read(in1_w, writing_c,  db_nitems * sizeof(writing_c[0]));
   if(r < 0) perror("Read error");

		 concatanate_index("preprocflags/party1_write_flags_d", tmp, 0);
		 int const in2_w { open( tmp, O_RDONLY ) };
		 r = read(in2_w, writing_d,  db_nitems * sizeof(writing_d[0]));	
	  if(r < 0) perror("Read error");
	}

	 return 0;	
}

 int read_flags_for_refreshing(size_t db_nitems)
 {
   char tmp[100];
   concatanate_index("preprocflags/P2_party0_write_flags_c", tmp, 0);
   int const in1_w { open(tmp, O_RDONLY ) };
   size_t r = read(in1_w, writing_c,  db_nitems * sizeof(writing_c[0]));
   if(r < 0) perror("Read error");
   
   concatanate_index("preprocflags/P2_party1_write_flags_d", tmp, 0);
   int const in2_w { open(tmp, O_RDONLY ) };
   r = read(in2_w, writing_d,  db_nitems * sizeof(writing_d[0])); 
   if(r < 0) perror("Read error");

   concatanate_index("preprocflags/P2_party0_write_c", tmp, 0);
   int const in1_w_ { open(tmp, O_RDONLY ) };
   r = read(in1_w_, c,  db_nitems * sizeof(c[0]));
   if(r < 0) perror("Read error");
   
   concatanate_index("preprocflags/P2_party1_write_d", tmp, 0);
   int const in2_w_ { open(tmp, O_RDONLY ) };
   r = read(in2_w_, d,  db_nitems * sizeof(d[0]));  
   if(r < 0) perror("Read error");

   return 0;
 }

int read_flags_for_generating_cancellation_terms(size_t db_nitems)
{
    char tmp[100];
    concatanate_index("preprocflags/P2_party1_read_flags_d", tmp, 0);
    int const in2 { open(tmp, O_RDONLY ) };
    size_t r = read(in2, reading_d,  db_nitems * sizeof(reading_d[0])); 
    if(r < 0) perror("Read error");

    concatanate_index("preprocflags/P2_party0_read_flags_c", tmp, 0);
    int const in2_ { open(tmp, O_RDONLY ) };
    r = read(in2_, reading_c,  db_nitems * sizeof(reading_c[0])); 
    if(r < 0) perror("Read error");
   
   return 0;
}


int read_flags_for_updating(bool party, size_t db_nitems)
{
	if(!party)
	{
		 int const in0_w_ { open( "preprocflags/party0_write_b0", O_RDONLY ) };
		 size_t r = read(in0_w_, b,  db_nitems * sizeof(b[0]));	
	  if(r < 0) perror("Read error");
 	
   int const in1_w_ { open( "preprocflags/party0_write_c0", O_RDONLY ) };
		 r = read(in1_w_, c,  db_nitems * sizeof(c[0]));
   if(r < 0) perror("Read error");
		 
   int const in2_w_ { open( "preprocflags/party0_write_d0", O_RDONLY ) };
		 r = read(in2_w_, d,  db_nitems * sizeof(d[0]));	
   if(r < 0) perror("Read error");
	}

	if(party)
	{
 	int const in0_w_ { open( "preprocflags/party1_write_b0", O_RDONLY ) };
	 size_t r = read(in0_w_, b,  db_nitems * sizeof(b[0]));	
  if(r < 0) perror("Read error");
 	
  int const in1_w_ { open( "preprocflags/party1_write_c0", O_RDONLY ) };
		r = read(in1_w_, c,  db_nitems * sizeof(c[0]));
  if(r < 0) perror("Read error");
		
  int const in2_w_ { open( "preprocflags/party1_write_d0", O_RDONLY ) };
		 r = read(in2_w_, d,  db_nitems * sizeof(d[0]));	
  if(r < 0) perror("Read error");
	}

 
	return 0;	
}