Public paste
c++
By: jazy84 | Date: Nov 22 2007 01:09 | Format: C++ | Expires: never | Size: 1018 B | Hits: 1349

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9. string dectobin(int num,int base,int byte);
  10. //string convert(int bits);
  11.  
  12.  
  13. int main() {
  14.   ifstream inFile;
  15.   ofstream outFile;
  16.   //input file stream
  17.   //output file stream
  18.   string inFileName,outFileName;
  19.   inFileName = "input6.txt";
  20.   outFileName = "output6.txt";
  21.  
  22.   inFile.open(inFileName.c_str());
  23.   outFile.open(outFileName.c_str());
  24.   int decimalNumber,bit;
  25.   int const base = 2;
  26.  
  27.   /* checking if file input and is valid. Doesn't matter about outFile, since it will be created */
  28.  
  29.   if (!inFile) {
  30.     cerr<<"Can't open Input file '"<<inFileName<<"'n";
  31.     return 1;
  32.   }
  33.  
  34.   inFile >> bit;
  35.   cout<<"Bit: "<<bit<<endl;
  36.   inFile >> decimalNumber;
  37.  
  38.   while(!inFile.eof()) {  
  39.     cout<<"Dec: "<<decimalNumber<<" = " <<dectobin(decimalNumber, base, bit) <<endl;
  40.     outFile<<"Decimal: "<<decimalNumber<<" = "<<dectobin(decimalNumber, base, bit)<<endl;
  41.     inFile >> decimalNumber;
  42.   }