Advertisement
Guest User

Binary I/O

a guest
Nov 23rd, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. /**
  2.  *  @file p2.cpp
  3.  *
  4.  *  CS 150 - Command Line Arguments and Binary Files
  5.  */
  6.  
  7. /*
  8. 1. No arguments supplied? Print an error message on standard error and exit
  9.  
  10. 2. One argument? Open the input file in binary mode and print (on the console)
  11. the name of the file followed by its size in bytes. See format below.
  12.     • Cannot open file? Print error message (standard error) and exit
  13. 3. Two arguments? Open the second file for output (in text mode) and the
  14. information from the first file in this file instead of to the screen.
  15.     • Cannot open file for output? Print error message (standard error) and exit
  16. */
  17.  
  18. #include <iostream>
  19. #include <fstream>
  20. #include <sstream>
  21. #include <string>
  22. #include <cstdlib>
  23. using namespace std;
  24.  
  25. //////// FUNCTION PROTOTYPES
  26.  
  27. bool printBinarySize(char * filename, ostream& out);
  28.  
  29. ////////////////////////////
  30.  
  31. //////////////////////////// DEFINE YOUR MAIN FUNCTION BELOW THIS LINE
  32.  
  33. int main(int argc, char * argv[])
  34. {
  35.     switch (argc)
  36.     {
  37.     case 1:
  38.         cerr << "Usage: p2 file-to-open [file-to-save-to]" << endl;
  39.         return 1;
  40.  
  41.         break;
  42.    
  43.     case 2:
  44.         if (! printBinarySize(argv[1], cout)) // it still executes
  45.             return 1;
  46.  
  47.         break;
  48.  
  49.     case 3:
  50.         ofstream out(argv[2]);
  51.        
  52.         if (out.fail())
  53.         {
  54.             cerr << "Cannot open the output file " << argv[2] << " for writing.";
  55.             return 1;
  56.         }
  57.        
  58.         if (! printBinarySize(argv[1], out)) // it still executes
  59.             return 1;  
  60.      
  61.         break;
  62.  
  63.     }
  64.    
  65.     return 0;
  66. }
  67.  
  68.  
  69.  
  70. ////////////// FUNCTIONS
  71.  
  72. /*
  73.     Function that opens a binary file, checks if it could be open,
  74.     gets its size in bytes, and then send the size to an ostream
  75.     object.
  76.  
  77.     Returns false if it could not open the file and true if succeded.
  78. */
  79. bool printBinarySize(char * filename, ostream& out)
  80. {
  81.     ifstream in(filename, ios::binary | ios::in | ios::ate); //open to read and at the end
  82.    
  83.     if (in.fail())
  84.     {
  85.         cerr << "Cannot open the input file " << filename;
  86.         return false;
  87.     }
  88.  
  89.     ios::pos_type size = in.tellg(); // get posicion ATE (At The End)
  90.     out << filename << ": size is " << size << " bytes.";
  91.  
  92.     return true;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement