Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么';如果流读到最后?_C++_Std_Fstream - Fatal编程技术网

C++ 为什么';如果流读到最后?

C++ 为什么';如果流读到最后?,c++,std,fstream,C++,Std,Fstream,我正在做一个类似记事本的程序。为了从文件中获取文本,我通过执行以下操作将每个字符读入缓冲区 while (!file.EOF()) { mystr += file.get(); } 但是,如果我加载一个exe,它会在MZ之后停止,但记事本会读取整个exe。 我将ifstream设置为二进制模式,但仍然不走运。我做错了什么? 谢谢 代码: (凌乱) void LoadTextFromString(HWND-ctrl,char*dirtypath,bool-noquotes) { char*F

我正在做一个类似记事本的程序。为了从文件中获取文本,我通过执行以下操作将每个字符读入缓冲区

while (!file.EOF())
{
  mystr += file.get();
}
但是,如果我加载一个exe,它会在MZ之后停止,但记事本会读取整个exe。 我将ifstream设置为二进制模式,但仍然不走运。我做错了什么? 谢谢

代码: (凌乱)

void LoadTextFromString(HWND-ctrl,char*dirtypath,bool-noquotes)
{
char*FileBuffer;
char*buf;
整数计数;
计数=0;
bool-hasDot=false;
矢量引号;
矢量文件;
字符串温度;
如果(无引号)
{
野曲后藤;
}
while(dirtypath[count]!=0)
{
if(dirtypath[count]==34)
{
引号。推回(计数);
}
计数+=1;
}
if(quotes.size()<3 | | quotes.size()%2!=0)
{
返回;
}
对于(int i=0;i失败())
{
返回;
}
长度;
长度=0;
而(!tf->eof())
{
如果(tf->get()==10)
{
长度+=1;
}
长度+=1;
}
tf->close();
如果(长度==0)
{
SetWindowTextA(ctrl“”);
返回;
}
buf=新字符[长度+1];
内特莱恩;
lenn=0;
炭铬;
cr=10;
char-tmp;
而(!file->eof())
{
buf[lenn]=文件->获取();
if(buf[lenn]==cr)
{
tmp=13;
buf[lenn]=tmp;
buf[lenn+1]=cr;
lenn+=1;
}
lenn+=1;
}
buf[lenn-1]=0;
文件->读取(buf,lenn);
SetWindowTextA(ctrl,buf);
文件->关闭();
}
删除(buf);
}
但是,如果我加载一个exe,它就会停止 在MZ之后

.exe类型的文件可以包含所有类型的字节,即使是0,您需要在附加到字符串之前检查字节值


关于什么是MZ?你怎么知道它没有读到最后?MZ是PE exe头的acii版本,但它没有读到超过这个(即它将其视为行尾)请显示用于打开文件的代码。此外,isftream具有
eof
,而不是
eof
;这是一个输入错误,还是这里发生了其他事情?是不是控件在一个空终止字符后停止读取?是的,它工作了!谢谢大家!
void LoadTextFromString(HWND ctrl, char* dirtypath, bool noquotes)
{
char *FileBuffer;

char *buf;

int count;
count = 0;

bool hasDot = false;
vector<int> quotes;
vector<string> files;
string temp;


if (noquotes)
{

    goto noqu;
}

while(dirtypath[count] != 0)
{
    if (dirtypath[count] == 34)
    {
        quotes.push_back(count);
    }
    count +=1;
}
if (quotes.size() < 3 || quotes.size() % 2 != 0)
{
    return;
}

for (int i = 0; i < quotes.size(); i += 2)
{

    temp = "";
    for (int j = quotes[i] + 1; j < quotes[i + 1]; ++ j)
    {
        temp += dirtypath[j];
    }

    files.push_back(temp);
}

for(int i = 0; i < files.size(); ++i)
{
noqu:
if (!noquotes)
{
FileBuffer = (char*)files[i].c_str();
}
else
{
FileBuffer = dirtypath;
}



ifstream *tf;
tf = new ifstream(FileBuffer,ios::binary);

ifstream *file;
file = new ifstream(FileBuffer,ios::binary);
if(file->fail())
{
    return;
}
int thelength;
thelength = 0;

while (!tf->eof())
{
    if (tf->get() == 10)
    {
        thelength +=1;
    }

    thelength +=1;
}
tf->close();

if(thelength == 0)
{
    SetWindowTextA(ctrl,"");
    return;
}
buf = new char[thelength + 1];

int lenn;
lenn = 0;
char cr ;
cr = 10;
char tmp;

while (!file->eof())
{

    buf[lenn] = file->get();
    if (buf[lenn] == cr) 
    {
        tmp = 13;
        buf[lenn] = tmp;

        buf[lenn + 1] = cr;
        lenn += 1;
    }
    lenn += 1;



}
buf[lenn - 1] = 0;

file->read(buf,lenn);
SetWindowTextA(ctrl,buf);
file->close();

}
delete(buf);

}