Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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++中做算法大约3个月,现在作为一种爱好。我从来没有遇到过一个直到现在都无法通过谷歌搜索解决的问题。我试图从一个将被转换成哈希表的文本文件中读取数据,但当我试图从一个文件中捕获数据时,它以空格结尾。这是密码 #include <iostream> #include <fstream> int main() { using namespace std; ifstream file("this.hash"); file >> noskipws; string thing; file >> thing; cout << thing << endl; return 0; } #包括 #包括 int main() { 使用名称空间std; ifstream文件(“this.hash”); 文件>>noskipws; 弦的东西; 文件>>东西; cout_C++ - Fatal编程技术网 >noskipws; 弦的东西; 文件>>东西; cout,c++,C++" /> >noskipws; 弦的东西; 文件>>东西; cout,c++,C++" />

将包含空格的文件中的文本存储为字符串 因此,我一直在C++中做算法大约3个月,现在作为一种爱好。我从来没有遇到过一个直到现在都无法通过谷歌搜索解决的问题。我试图从一个将被转换成哈希表的文本文件中读取数据,但当我试图从一个文件中捕获数据时,它以空格结尾。这是密码 #include <iostream> #include <fstream> int main() { using namespace std; ifstream file("this.hash"); file >> noskipws; string thing; file >> thing; cout << thing << endl; return 0; } #包括 #包括 int main() { 使用名称空间std; ifstream文件(“this.hash”); 文件>>noskipws; 弦的东西; 文件>>东西; cout

将包含空格的文件中的文本存储为字符串 因此,我一直在C++中做算法大约3个月,现在作为一种爱好。我从来没有遇到过一个直到现在都无法通过谷歌搜索解决的问题。我试图从一个将被转换成哈希表的文本文件中读取数据,但当我试图从一个文件中捕获数据时,它以空格结尾。这是密码 #include <iostream> #include <fstream> int main() { using namespace std; ifstream file("this.hash"); file >> noskipws; string thing; file >> thing; cout << thing << endl; return 0; } #包括 #包括 int main() { 使用名称空间std; ifstream文件(“this.hash”); 文件>>noskipws; 弦的东西; 文件>>东西; cout,c++,C++,当为std::string使用格式化输入运算符时,它总是在流认为是空白的地方停止。使用std::locale的字符分类方面std::ctype可以重新定义空格的含义。不过这有点复杂 如果要读取特定的分隔符,可以使用std::getline(),可能需要指定您感兴趣的分隔符,例如: std::string value; if (std::getline(in, value, ',')) { ... } 读取字符,直到找到逗号或到达文件末尾,并将字符存储到值中的分隔符处 如果您只想读取整个文件,一

当为
std::string
使用格式化输入运算符时,它总是在流认为是空白的地方停止。使用
std::locale
的字符分类方面
std::ctype
可以重新定义空格的含义。不过这有点复杂

如果要读取特定的分隔符,可以使用
std::getline()
,可能需要指定您感兴趣的分隔符,例如:

std::string value;
if (std::getline(in, value, ',')) { ... }
读取字符,直到找到逗号或到达文件末尾,并将字符存储到
值中的分隔符处

如果您只想读取整个文件,一种方法是使用

std::ifstream in(file.c_str());
std::string   all((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
std::ifstream-in(file.c_str());
std::string all((std::istreambuf_迭代器(in)),std::istreambuf_迭代器();

当为
std::string
使用格式化输入运算符时,它总是在流认为是空白的地方停止。使用
std::locale
的字符分类方面
std::ctype
可以重新定义空格的含义。不过这有点复杂

如果要读取特定的分隔符,可以使用
std::getline()
,可能需要指定您感兴趣的分隔符,例如:

std::string value;
if (std::getline(in, value, ',')) { ... }
读取字符,直到找到逗号或到达文件末尾,并将字符存储到
值中的分隔符处

如果您只想读取整个文件,一种方法是使用

std::ifstream in(file.c_str());
std::string   all((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
std::ifstream-in(file.c_str());
std::string all((std::istreambuf_迭代器(in)),std::istreambuf_迭代器();

我认为最好的工具是
get
getline
read
。现在这些工具都使用
char
缓冲区,而不是
std::string
缓冲区,所以需要多考虑一下,但它们实际上非常简单。(编辑:
std::getline(文件,字符串),如Dietmar K席HL所指出的,使用C++字符串而不是字符缓冲区,所以我实际上会建议它。那么,您不必担心最大行长度)

下面是一个将在整个文件中循环的示例:

#include <iostream>

int main () {
    char buffer[1024]; // line length is limited to 1023 bytes

    std::ifstream file( "this.hash" );

    while( file.good( ) ) {
        file.getline( buffer, sizeof( buffer ) );
        std::string line( buffer ); // convert to c++ string for convenience
        // do something with the line
    }

    return 0;
}
#包括
int main(){
字符缓冲区[1024];//行长度限制为1023字节
std::ifstream文件(“this.hash”);
while(file.good()){
getline(buffer,sizeof(buffer));
STD::String行(缓冲区);//转换为C++字符串以方便
//用绳子做点什么
}
返回0;
}
(请注意,行长限制为1023字节,如果一行较长,它将被分为两次读取。当它是真正的换行符时,您将在字符串末尾看到一个
\n
字符)

当然,如果您事先为文件指定了最大长度,您可以相应地设置缓冲区并取消循环。如果缓冲区需要非常大(超过几千字节),您可能应该使用
new char[size]
delete[]
来避免堆栈溢出


这里有一个参考页面:

我认为你想要做的最好的工具是
get
getline
read
。现在这些工具都使用
char
缓冲区,而不是
std::string
缓冲区,所以需要更多的思考,但它们实际上非常简单。(编辑:
std::getline(文件,字符串),如Dietmar K席HL所指出的,使用C++字符串而不是字符缓冲区,所以我实际上会建议它。那么,您不必担心最大行长度)

下面是一个将在整个文件中循环的示例:

#include <iostream>

int main () {
    char buffer[1024]; // line length is limited to 1023 bytes

    std::ifstream file( "this.hash" );

    while( file.good( ) ) {
        file.getline( buffer, sizeof( buffer ) );
        std::string line( buffer ); // convert to c++ string for convenience
        // do something with the line
    }

    return 0;
}
#包括
int main(){
字符缓冲区[1024];//行长度限制为1023字节
std::ifstream文件(“this.hash”);
while(file.good()){
getline(buffer,sizeof(buffer));
STD::String行(缓冲区);//转换为C++字符串以方便
//用绳子做点什么
}
返回0;
}
(请注意,行长限制为1023字节,如果一行较长,它将被分为两次读取。当它是真正的换行符时,您将在字符串末尾看到一个
\n
字符)

当然,如果您事先为文件指定了最大长度,您可以相应地设置缓冲区并取消循环。如果缓冲区需要非常大(超过几千字节),您可能应该使用
new char[size]
delete[]
来避免堆栈溢出


这里有一个参考页面:

noskipws
只意味着前面的空白将被包括在内,因此,例如,“hello”将被读为“hello
,而不是“hello
”。它对阅读
hello fred>没有任何作用。(这没有格式化,但希望你能理解)实际上,如果读取字符串以空格开头,那么您可能会发现设置
noskipws
会使它完全不读取任何内容,因为它会立即找到一个终止字符。如何加载文本文件?一次一行?尝试
getline
noskipws
只意味着前面的空白将被包括在内,因此,例如,“hello”将被解读为“hello
,而不是“hello
”。它对阅读“hello fred”没有任何作用(这没有格式化,但希望你能理解),实际上,如果读str