rihardmarius

libreria archivos

Nov 24th, 2013
47
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <string>
  5. #include <array>
  6.  
  7. template<typename T>
  8. std::ostream& writeblock(std::ostream& out, const T& block){
  9.     return out.write(
  10.         reinterpret_cast<const char*>(&block),
  11.         sizeof block
  12.     );
  13. }
  14.  
  15.  
  16. template<typename T>
  17. std::istream& readblock(std::istream& in, T& block){
  18.     return in.read(
  19.         reinterpret_cast<char*>(&block),
  20.         sizeof block
  21.     );
  22. }
  23.  
  24. template<typename T>
  25. std::string packstring(T& a, const std::string& s)
  26. {
  27.     if (s.length() >= a.size()) // no entra
  28.         a.at(a.size()-1) = '\0'; // pone un 0 en el ultimo
  29.     else
  30.         a.at(s.length()) = '\0'; // pone un 0 despues del string
  31.     s.copy(a.data(), a.size()-1); // donde copiar, cuanto copiar
  32.     return s;
  33. }
  34.  
  35. template<typename T>
  36. std::string unpackstring(const T& a){
  37.     return std::string(a.data(), a.size());
  38. }
  39.  
  40. template<typename stream_type>
  41. void check_open (const stream_type& file)
  42. {
  43.     if (not file.is_open())
  44.     {
  45.         std::cout << "Archivo no abierto." << std::endl;
  46.         std::exit(1);
  47.     }
  48. }
  49.  
  50. template<typename stream_type>
  51. void mostrar_estado (const stream_type& file)
  52. {
  53.     if (file.bad())
  54.         std::cout << "I/O error while reading\n";
  55.     else if (file.eof())
  56.         std::cout << "End of file reached successfully\n";
  57.     else if (file.fail())
  58.         std::cout << "Non-integer data encountered\n";
  59.     std::cout << std::endl;
  60. }
  61.  
  62. template<typename stream_type>
  63. void close_file (stream_type& file)
  64. {
  65.     file.close();
  66. }
RAW Paste Data