读取 MAC 地址时文件读取异常
File read exception while reading MAC address
我们有一个通用的文件操作类,它将执行所有基本的文件操作。所以我使用相同的文件操作类从Linux机器读取MAC地址,它抛出basic_ios::clear:iostream异常。
这是将执行文件操作的代码
bool FileIO::ReadTextFile(const std::string & FileName, std::string & Contents)
{
bool Result = false;
std::ifstream FileObj;
try
{
FileObj.exceptions(std::ifstream::badbit | std::ifstream::failbit);
if(DoesFileExist(FileName))
{
FileObj.open(FileName, std::ifstream::in);
FileObj.seekg(0, std::ios::end);
Contents.resize(FileObj.tellg());
FileObj.seekg(0, std::ios::beg);
FileObj.read(&Contents[0], Contents.size());
FileObj.close();
Result = true;
}
}
catch (std::exception & e)
{
std::cout << "Error when reading from file : " << FileName << " "<< std::strerror(errno) << " Exception : " << e.what() << std::endl;
}
return Result;
}
我像下面这样调用这个函数,
std::string MACAddress;
pFOpHandler->ReadEntireTextFile("/sys/class/net/eth0/address", MACAddress);
它已成功读取 MAC 地址,但文件操作引发异常,并且 MACAddress 字符串包含 MAC 地址和一些垃圾值。
你可能想试试这个。您必须为 fstream 和 sstream 添加包含文件。
bool FileIO::ReadTextFile(const std::string &FileName, std::string &Contents) {
bool Result = false;
std::ifstream FileObj;
try {
FileObj.exceptions(std::ifstream::badbit | std::ifstream::failbit);
if (DoesFileExist(FileName)) {
FileObj.open(FileName, std::ifstream::in);
std::stringstream FileContents;
FileContents << FileObj.rdbuf();
Contents = FileContents.str();
Result = true;
}
} catch (std::exception &e) {
std::cout << "Error when reading from file : " << FileName << " "
<< std::strerror(errno) << " Exception : " << e.what()
<< std::endl;
}
相关文章:
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 读取文件时引发异常
- 从二进制文件中读取元素时引发异常(引发异常:读取访问冲突. _Pnext 0xB414D4)
- 引发异常:读取访问冲突.**此**0x40错误
- (此函数用于计算 SMA).在 FA.exe 中0x00509159引发异常: 0xC0000005:访问冲突读取位置0
- 读取 MAC 地址时文件读取异常
- free():在有效指针异常中-使用流读取巨大文件时
- Visual Studio图形调试器引发读取访问冲突异常
- C++中的内存分配(引发异常:读取访问冲突)
- 引发异常:读取访问冲突 [C++]
- 0x0F50DF58:0xC0000005:访问冲突读取位置0x0047CA04时未处理的异常
- 引发未经处理的异常:简单 C++ 程序中的读取访问冲突,动态增加数组长度以存储数字
- 错误 : 异常 : 引发未处理的异常: 读取访问冲突.0xDDDDDDDD临时温度
- 媒体基础:读取示例 - 访问冲突异常
- 挂钩创建文件抛出异常:读取访问冲突
- 仅捕获异常就可以检测所有二进制文件在C 中读取错误是否足够
- 我无法理解此异常:读取访问冲突
- 删除函数 c++ 的读取访问冲突异常
- 实验室中0x6BE20E11 (ucrtbased.dll) 的未处理异常.exe: 0xC0000005:访问冲突读取
- 引发异常:读取访问冲突.**_Right_data**0x8.发生