Advertisement
dimon2242

Untitled

Jul 1st, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. // Code/decode symbols from cp1251 by their codes
  2.  
  3. //#include "stdafx.h"
  4. #include <iostream>
  5. #include <cstdio>
  6. //#include <Windows.h>
  7. #include <fstream>
  8. #include <cstring>
  9.  
  10. #define ADDSIZE 10 // memory alloc defined
  11.  
  12. using namespace std;
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16.     short int start_ans;
  17.     if(argc == 2)
  18.         start_ans = 1;
  19.  
  20.     cout << "Hello there! This program can CODE/DECODE text\n";
  21.     do
  22.     {
  23.         cout << "\nWhat do you want to do?\n\n"
  24.             << " [1] Open file with text\n [2] Write text here\n\n"
  25.             << "Your answer will be: ";
  26.         cin >> start_ans;
  27.     } while (start_ans != 1 && start_ans != 2);
  28.  
  29.     if (start_ans == 1)
  30.     {
  31.         // File name
  32.  
  33.         /*char f_name[90];
  34.         cout << "\nEnter file full path\nor name: ";
  35.         fflush(stdin);
  36.         //gets(f_name); // dangerous
  37.         while((fgets(f_name, sizeof(f_name)-1, stdin)) != NULL) { // fgets() will be safer
  38.             f_name[strlen(f_name)-1] = '\0';
  39.             fflush(stdin);
  40.         } */
  41.  
  42.         // Open file
  43.  
  44.         ifstream f0_stream(argv[1], ios::in | ios::binary);
  45.         if (!f0_stream)
  46.         {
  47.             cout << "\nError in opening...\n\n";
  48.             //system("pause");
  49.             return 1;
  50.         }
  51.         else
  52.             cout << "\nFile successfully opened!\n\n";
  53.  
  54.         // Search chars number in file
  55.  
  56.         register short int chs_in_file = 0;
  57.         char test;
  58.         while (f0_stream.get(test)) chs_in_file++;
  59.         cout  << "chars: " << chs_in_file << "\n\n";
  60.  
  61.         // Rewind f_srteam:
  62.  
  63.         f0_stream.close();
  64.         ifstream f_stream(argv[1], ios::in | ios::binary);
  65.         if (!f_stream)
  66.             return 1;
  67.  
  68.         // Allocate memory
  69.         cout << "chs_in_file + " << ADDSIZE << ": " << chs_in_file + ADDSIZE;
  70.         char *ptr_file_buf = new char [chs_in_file + ADDSIZE];
  71.         /*if (!*ptr_file_buf)
  72.         {
  73.             cout << "\nError in memory allocating...\n\n";
  74.             //system("pause");
  75.             return 1;
  76.         } */
  77.  
  78.         // Copy information in buffer & preview
  79.  
  80.         //system("pause");
  81.         cout << "\n''" << argv[1] << "'' preview:\n\n";
  82.         register short int i = 0;
  83.         while (f_stream.get(*ptr_file_buf))
  84.         {
  85.             cout << endl << ++i << endl; // Number of char we working on
  86.             if (i <= 100) cout << *ptr_file_buf;
  87.             //ptr_file_buf++; // Do not use, iteration already in while();
  88.         }
  89.         *ptr_file_buf = '\0';
  90.  
  91.         if (i > 100) cout << "\n\n... [etc.]\n\n";
  92.         delete [] ptr_file_buf; //                                                      !!! HERE IS A TRABL
  93.  
  94.     }
  95.  
  96.     //system("pause");
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement