Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code/decode symbols from cp1251 by their codes
- //#include "stdafx.h"
- #include <iostream>
- #include <cstdio>
- //#include <Windows.h>
- #include <fstream>
- #include <cstring>
- #define ADDSIZE 10 // memory alloc defined
- using namespace std;
- int main(int argc, char* argv[])
- {
- short int start_ans;
- if(argc == 2)
- start_ans = 1;
- cout << "Hello there! This program can CODE/DECODE text\n";
- do
- {
- cout << "\nWhat do you want to do?\n\n"
- << " [1] Open file with text\n [2] Write text here\n\n"
- << "Your answer will be: ";
- cin >> start_ans;
- } while (start_ans != 1 && start_ans != 2);
- if (start_ans == 1)
- {
- // File name
- /*char f_name[90];
- cout << "\nEnter file full path\nor name: ";
- fflush(stdin);
- //gets(f_name); // dangerous
- while((fgets(f_name, sizeof(f_name)-1, stdin)) != NULL) { // fgets() will be safer
- f_name[strlen(f_name)-1] = '\0';
- fflush(stdin);
- } */
- // Open file
- ifstream f0_stream(argv[1], ios::in | ios::binary);
- if (!f0_stream)
- {
- cout << "\nError in opening...\n\n";
- //system("pause");
- return 1;
- }
- else
- cout << "\nFile successfully opened!\n\n";
- // Search chars number in file
- register short int chs_in_file = 0;
- char test;
- while (f0_stream.get(test)) chs_in_file++;
- cout << "chars: " << chs_in_file << "\n\n";
- // Rewind f_srteam:
- f0_stream.close();
- ifstream f_stream(argv[1], ios::in | ios::binary);
- if (!f_stream)
- return 1;
- // Allocate memory
- cout << "chs_in_file + " << ADDSIZE << ": " << chs_in_file + ADDSIZE;
- char *ptr_file_buf = new char [chs_in_file + ADDSIZE];
- /*if (!*ptr_file_buf)
- {
- cout << "\nError in memory allocating...\n\n";
- //system("pause");
- return 1;
- } */
- // Copy information in buffer & preview
- //system("pause");
- cout << "\n''" << argv[1] << "'' preview:\n\n";
- register short int i = 0;
- while (f_stream.get(*ptr_file_buf))
- {
- cout << endl << ++i << endl; // Number of char we working on
- if (i <= 100) cout << *ptr_file_buf;
- //ptr_file_buf++; // Do not use, iteration already in while();
- }
- *ptr_file_buf = '\0';
- if (i > 100) cout << "\n\n... [etc.]\n\n";
- delete [] ptr_file_buf; // !!! HERE IS A TRABL
- }
- //system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement