![]() |
#2
apull2022-09-09 15:02
|
网上源代码地址: https://blog.
只有本站会员才能查看附件,请 登录
问题1: 反复检查,没找到错误。
错误代码也看不懂。
只有本站会员才能查看附件,请 登录
问题2:代码从第6行到第9行看不懂!!!
单独的一行#define 宏替换勉强可以看懂。
单独的一行typedef 用户定义类型勉强可以看懂。
用'\'把它们连在一起,里面还有不止一个';'分号。
宏替换末尾没有';'分号。
这几个问题,凑在一起,彻底蒙圈了。。。。

#include<iostream>
#include<locale.h>
#pragma warning(disable:4996)
#define DISALLOW_COPY_AND_ASSIGN(typename)\
private:\
typedef(typename&);\
typename& operator=(typename&);
using namespace std;
typedef unsigned int Uint32;
class File {
public:
File() { _ptr_file = nullptr; }
File(const char* fn, const char* opr) {
open(fn, opr);
}
~File() { close();}
//读取文件到内存
static char* read_file(Uint32& size, const char* fn) {
File tmp_file;
if (tmp_file.open(fn, "rb")) { return size = 0, nullptr; }
size = tmp_file.get_file_size();
char* buf = (char*)malloc(size + 4);///这里多分配几个字节做字符串结尾
if (!buf) { return size = 0, nullptr; }
memset(buf, 0, size + 4); ///内存填充0
size = tmp_file.read_byte(buf, size);
return buf;
}
///获取文本文件类型
///文件类型:utf-8返回0
/// utf8 bom 返回1
/// ucs-2 BE大端字节序返回2
/// USC-2 LE小端字节序返回3
/// 文件不存在返回 -1
static int get_file_type(const char* fn) {
File temp_file;
if (temp_file.open(fn, "rb")) { return -1; }
if (temp_file.get_file_size() <= 2) { return 0; }
char en_buf[3] = { 0 };
temp_file.read_byte(en_buf, 3);
if (0 == memcmp(en_buf, "\xEF\xBB\xBF", 3)) { return 1; }
else if (0 == memcmp(en_buf, "\xFE\xFF", 2)) { return 1; }
else if (0 == memcmp(en_buf, "\xFF\xFE", 2)) { return 1; }
return 0;
}
int open(const char* fn, const char* opr) {
close();
_ptr_file = fopen(fn, opr);
if (!_ptr_file) { return-1; }
return 0;
}
void close() {
if (!_ptr_file) { return; }
fclose((FILE*)_ptr_file);
}
///获取文件大小
Uint32 get_file_size() {
int cur_pos = ftell((FILE*)_ptr_file);
fseek((FILE*)_ptr_file, 0L, SEEK_END);
Uint32 ret = ftell((FILE*)_ptr_file);
fseek((FILE*)_ptr_file, cur_pos, SEEK_SET);
return ret;
}
///读取size个字节到dst///
Uint32 read_byte(char* dst, Uint32 size) {
Uint32 min_size = min(size, get_file_size());
fread(dst, 1, min_size, (FILE*)_ptr_file);
return min_size;
}
private:
void* _ptr_file;
DISALLOW_COPY_AND_ASSIGN(File)
};
///大端字节序转为小端字节序,当前大部分笔记本和手机都是小端字节序
void big2little(wchar_t* src, Uint32 size) {
for (Uint32 iix = 0; iix < size; ++iix, ++src) {
*src = (((*src) & 0xff00) >> 8 | (((*src) & 0x00ff) << 8));
}
return;
}
int main() {
const char* filename = "C:\\Users\\Administrator\\Desktop\\B.txt";
Uint32 size = 0;
char* buffer = File::read_file(size, filename);
if (0 == size && nullptr == buffer) { cout << "文件不存在!!!" << endl; }
int filetype = File::get_file_type(filename);
_wsetlocale(LC_ALL, L"chinese");
switch (filetype) {
case 0: //utf8编码
cout << std::string(buffer);
break;
case 1: //utf8 bom编码
cout << std::string(buffer + 3);
break;
case 2: //ucs-2 BE
big2little((wchar_t*)buffer + 1, size / 2 - 1);
wcout << std::wstring((wchar_t*)buffer + 1);
break;
case 3: //ucs-2 LE
wcout << std::wstring((wchar_t*)buffer + 1);
break;
default:
cout << "文件不存在!!!" << endl;
}
free(buffer);
return 0;
}
#include<locale.h>
#pragma warning(disable:4996)
#define DISALLOW_COPY_AND_ASSIGN(typename)\
private:\
typedef(typename&);\
typename& operator=(typename&);
using namespace std;
typedef unsigned int Uint32;
class File {
public:
File() { _ptr_file = nullptr; }
File(const char* fn, const char* opr) {
open(fn, opr);
}
~File() { close();}
//读取文件到内存
static char* read_file(Uint32& size, const char* fn) {
File tmp_file;
if (tmp_file.open(fn, "rb")) { return size = 0, nullptr; }
size = tmp_file.get_file_size();
char* buf = (char*)malloc(size + 4);///这里多分配几个字节做字符串结尾
if (!buf) { return size = 0, nullptr; }
memset(buf, 0, size + 4); ///内存填充0
size = tmp_file.read_byte(buf, size);
return buf;
}
///获取文本文件类型
///文件类型:utf-8返回0
/// utf8 bom 返回1
/// ucs-2 BE大端字节序返回2
/// USC-2 LE小端字节序返回3
/// 文件不存在返回 -1
static int get_file_type(const char* fn) {
File temp_file;
if (temp_file.open(fn, "rb")) { return -1; }
if (temp_file.get_file_size() <= 2) { return 0; }
char en_buf[3] = { 0 };
temp_file.read_byte(en_buf, 3);
if (0 == memcmp(en_buf, "\xEF\xBB\xBF", 3)) { return 1; }
else if (0 == memcmp(en_buf, "\xFE\xFF", 2)) { return 1; }
else if (0 == memcmp(en_buf, "\xFF\xFE", 2)) { return 1; }
return 0;
}
int open(const char* fn, const char* opr) {
close();
_ptr_file = fopen(fn, opr);
if (!_ptr_file) { return-1; }
return 0;
}
void close() {
if (!_ptr_file) { return; }
fclose((FILE*)_ptr_file);
}
///获取文件大小
Uint32 get_file_size() {
int cur_pos = ftell((FILE*)_ptr_file);
fseek((FILE*)_ptr_file, 0L, SEEK_END);
Uint32 ret = ftell((FILE*)_ptr_file);
fseek((FILE*)_ptr_file, cur_pos, SEEK_SET);
return ret;
}
///读取size个字节到dst///
Uint32 read_byte(char* dst, Uint32 size) {
Uint32 min_size = min(size, get_file_size());
fread(dst, 1, min_size, (FILE*)_ptr_file);
return min_size;
}
private:
void* _ptr_file;
DISALLOW_COPY_AND_ASSIGN(File)
};
///大端字节序转为小端字节序,当前大部分笔记本和手机都是小端字节序
void big2little(wchar_t* src, Uint32 size) {
for (Uint32 iix = 0; iix < size; ++iix, ++src) {
*src = (((*src) & 0xff00) >> 8 | (((*src) & 0x00ff) << 8));
}
return;
}
int main() {
const char* filename = "C:\\Users\\Administrator\\Desktop\\B.txt";
Uint32 size = 0;
char* buffer = File::read_file(size, filename);
if (0 == size && nullptr == buffer) { cout << "文件不存在!!!" << endl; }
int filetype = File::get_file_type(filename);
_wsetlocale(LC_ALL, L"chinese");
switch (filetype) {
case 0: //utf8编码
cout << std::string(buffer);
break;
case 1: //utf8 bom编码
cout << std::string(buffer + 3);
break;
case 2: //ucs-2 BE
big2little((wchar_t*)buffer + 1, size / 2 - 1);
wcout << std::wstring((wchar_t*)buffer + 1);
break;
case 3: //ucs-2 LE
wcout << std::wstring((wchar_t*)buffer + 1);
break;
default:
cout << "文件不存在!!!" << endl;
}
free(buffer);
return 0;
}
只有本站会员才能查看附件,请 登录