Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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++;_C++_Fstream - Fatal编程技术网

C++ 从二进制文件c++;

C++ 从二进制文件c++;,c++,fstream,C++,Fstream,正如标题所说,我试图从二进制文件中读取以null结尾的字符串 std::string ObjElement::ReadStringFromStream(std::ifstream &stream) { std::string result = ""; char ch; while (stream.get(ch) != '\0') { result += ch; } return result; } 我的空字符是“\0” 但每当我调

正如标题所说,我试图从二进制文件中读取以null结尾的字符串

std::string ObjElement::ReadStringFromStream(std::ifstream &stream) {
    std::string result = "";
    char ch;
    while (stream.get(ch) != '\0') {
        result += ch;
    }
    return result; }
我的空字符是“\0”

但每当我调用该方法时,它都会读取到文件的末尾

std::ifstream myFile(file, std::ios_base::in | std::ios_base::binary);
myFile.seekg(startByte);

this->name = ObjElement::ReadStringFromStream(myFile);
知道我这里做错了什么吗?

istream::get(char&)返回对
istream
的引用,而不是读取的字符。您可以像这样使用
istream::get()
变量:

while ((ch = stream.get()) != '\0') {
    result += ch;
}
或者将返回的流引用用作
bool

while (stream.get(ch)) {
    if (ch != '\0') {
        result += ch;
    } else {
        break;
    }
}
istream::get(char&)
返回对
istream
的引用,而不是读取的字符。您可以像这样使用
istream::get()
变量:

while ((ch = stream.get()) != '\0') {
    result += ch;
}
或者将返回的流引用用作
bool

while (stream.get(ch)) {
    if (ch != '\0') {
        result += ch;
    } else {
        break;
    }
}

使用
std::getline

#include <string> // for std::getline

std::string ObjElement::ReadStringFromStream(std::ifstream &stream) {
    std::string s;
    std::getline(stream, s, '\0');
    return s;
}
#包含//用于std::getline
std::string对象::ReadStringFromStream(std::ifstream&stream){
std::字符串s;
std::getline(流,s,'\0');
返回s;
}

使用
std::getline

#include <string> // for std::getline

std::string ObjElement::ReadStringFromStream(std::ifstream &stream) {
    std::string s;
    std::getline(stream, s, '\0');
    return s;
}
#包含//用于std::getline
std::string对象::ReadStringFromStream(std::ifstream&stream){
std::字符串s;
std::getline(流,s,'\0');
返回s;
}
函数
get()
返回对流的引用,而不是放入
ch

您需要测试
ch
是否为“\0”。

函数返回对流的引用,而不是放入
ch


您需要测试
ch
是否为“\0”。

使用并传递
\0
(空字符)作为定界符。

使用并传递
\0
(空字符)作为定界符。

get函数返回流的引用,而不是读取字符

修改代码如下:

std::string ObjElement::ReadStringFromStream(std::ifstream &stream) {
    std::string result = "";
    while (stream.get(ch)) { // exit at EOF
        if (ch != '\0')
            result += ch;
        else
            break;  // Stop the loop when found a '\0'
    }
    return result; 
}

get函数返回流的引用,而不是读取字符

修改代码如下:

std::string ObjElement::ReadStringFromStream(std::ifstream &stream) {
    std::string result = "";
    while (stream.get(ch)) { // exit at EOF
        if (ch != '\0')
            result += ch;
        else
            break;  // Stop the loop when found a '\0'
    }
    return result; 
}

您不需要提供ios::in或显式地将空字符串设置为“”。
stream.get(ch)
不返回字符。我不是从文件的开头读取,我从其他地方获得了所需的开始字节。您确定文件中包含ASCII零字符吗?你是怎么得到/创建这个文件的?哦,我的开始字节不好。需要moar Coffee您不需要提供ios::in或显式将空字符串设置为“”。
stream.get(ch)
不返回字符。我不是从文件的开头读取,我从其他地方获得了所需的开始字节。您确定文件中包含ASCII零字符吗?你是怎么得到/创建这个文件的?哦,我的开始字节不好。需要莫阿咖啡吗?沮丧的选民能明确解释为什么这不起作用吗?是的,这比我的方式容易。投票人能否明确解释为什么这不起作用?是的,这比我的方式简单。投票时请留下评论。投票时请留下评论。啊,我知道我做了什么。可能就是这样啊,我知道我做了什么。也许没关系