Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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
从fstream中读取包含空格的整行 我目前正在做一个C++的小项目,目前我有点困惑。我需要使用ifstream in()从文件中读取一行中的一定数量的单词。现在的问题是它一直忽略空格。我需要计算文件中的空格数来计算字数。在()中是否有不忽略空白的内容 ifstream in("input.txt"); ofstream out("output.txt"); while(in.is_open() && in.good() && out.is_open()) { in >> temp; cout << tokencount(temp) << endl; } ifstream-in(“input.txt”); 流输出(“output.txt”); while(in.is_open()&&in.good()&&out.is_open()) { 在>>温度; cout_C++_Fstream - Fatal编程技术网 >温度; cout,c++,fstream,C++,Fstream" /> >温度; cout,c++,fstream,C++,Fstream" />

从fstream中读取包含空格的整行 我目前正在做一个C++的小项目,目前我有点困惑。我需要使用ifstream in()从文件中读取一行中的一定数量的单词。现在的问题是它一直忽略空格。我需要计算文件中的空格数来计算字数。在()中是否有不忽略空白的内容 ifstream in("input.txt"); ofstream out("output.txt"); while(in.is_open() && in.good() && out.is_open()) { in >> temp; cout << tokencount(temp) << endl; } ifstream-in(“input.txt”); 流输出(“output.txt”); while(in.is_open()&&in.good()&&out.is_open()) { 在>>温度; cout

从fstream中读取包含空格的整行 我目前正在做一个C++的小项目,目前我有点困惑。我需要使用ifstream in()从文件中读取一行中的一定数量的单词。现在的问题是它一直忽略空格。我需要计算文件中的空格数来计算字数。在()中是否有不忽略空白的内容 ifstream in("input.txt"); ofstream out("output.txt"); while(in.is_open() && in.good() && out.is_open()) { in >> temp; cout << tokencount(temp) << endl; } ifstream-in(“input.txt”); 流输出(“output.txt”); while(in.is_open()&&in.good()&&out.is_open()) { 在>>温度; cout,c++,fstream,C++,Fstream,要计算文件中的空格数: std::ifstream inFile("input.txt"); std::istreambuf_iterator<char> it (inFile), end; int numSpaces = std::count(it, end, ' '); std::ifstream inFile("input.txt"); std::istreambuf_iterator<char> it (inFile), end; int numWS = std

要计算文件中的空格数:

std::ifstream inFile("input.txt");
std::istreambuf_iterator<char> it (inFile), end;
int numSpaces = std::count(it, end, ' ');
std::ifstream inFile("input.txt");
std::istreambuf_iterator<char> it (inFile), end;
int numWS = std::count_if(it, end, (int(*)(int))std::isspace);
std::ifstream infle(“input.txt”);
std::istreambuf_迭代器it(infle),结束;
int numSpaces=std::count(it,end“”);
要计算文件中的空白字符数,请执行以下操作:

std::ifstream inFile("input.txt");
std::istreambuf_iterator<char> it (inFile), end;
int numSpaces = std::count(it, end, ' ');
std::ifstream inFile("input.txt");
std::istreambuf_iterator<char> it (inFile), end;
int numWS = std::count_if(it, end, (int(*)(int))std::isspace);
std::ifstream infle(“input.txt”);
std::istreambuf_迭代器it(infle),结束;
int numWS=std::count_if(it,end,(int(*)(int))std::isspace);
另一种方法是,您可以计算单词,而不是计算空格

std::ifstream-infle(“foo.txt”);
std::istream_迭代器it(infle),结束;
int numWords=std::距离(it,end);

要计算文件中的空格数,请执行以下操作:

std::ifstream inFile("input.txt");
std::istreambuf_iterator<char> it (inFile), end;
int numSpaces = std::count(it, end, ' ');
std::ifstream inFile("input.txt");
std::istreambuf_iterator<char> it (inFile), end;
int numWS = std::count_if(it, end, (int(*)(int))std::isspace);
std::ifstream infle(“input.txt”);
std::istreambuf_迭代器it(infle),结束;
int numSpaces=std::count(it,end“”);
要计算文件中的空白字符数,请执行以下操作:

std::ifstream inFile("input.txt");
std::istreambuf_iterator<char> it (inFile), end;
int numSpaces = std::count(it, end, ' ');
std::ifstream inFile("input.txt");
std::istreambuf_iterator<char> it (inFile), end;
int numWS = std::count_if(it, end, (int(*)(int))std::isspace);
std::ifstream infle(“input.txt”);
std::istreambuf_迭代器it(infle),结束;
int numWS=std::count_if(it,end,(int(*)(int))std::isspace);
另一种方法是,您可以计算单词,而不是计算空格

std::ifstream-infle(“foo.txt”);
std::istream_迭代器it(infle),结束;
int numWords=std::距离(it,end);
以下是我的做法:

std::ifstream fs("input.txt");
std::string line;
while (std::getline(fs, line)) {
    int numSpaces = std::count(line.begin(), line.end(), ' ');
}
一般来说,如果我必须为文件的每一行做一些事情,我会发现std::getline是最不挑剔的方式。如果我需要从那里开始的流操作符,我最终会用这一行生成一个stringstream。这远不是最有效的方式,但我通常更关心的是如何正确地处理它并继续生活就是为了这类事情。

我会这样做:

std::ifstream fs("input.txt");
std::string line;
while (std::getline(fs, line)) {
    int numSpaces = std::count(line.begin(), line.end(), ' ');
}

一般来说,如果我必须为文件的每一行做一些事情,我会发现std::getline是最不挑剔的方式。如果我需要从那里开始的流操作符,我最终会用这一行生成一个stringstream。这远不是最有效的方式,但我通常更关心的是如何正确地处理它并继续生活就是为了这类事情。

你可以使用
istreambuf\u迭代器
进行
count

ifstream fs("input.txt");

int num_spaces = count(istreambuf_iterator<unsigned char>(fs),
                       istreambuf_iterator<unsigned char>(),
                       ' ');
ifstream fs(“input.txt”);
int num_spaces=count(istreambuf_迭代器(fs),
istreambuf_迭代器(),
' ');
编辑

最初我的答案使用的是
istream\u迭代器
,但是作为
@Robᵩ指出它不起作用

istream\u迭代器
将在流上迭代,但采用空白格式并跳过它。上面的示例使用
istream\u迭代器
返回的结果为零,因为迭代器跳过了空白,然后我要求它计算剩余的空间

istreambuf_迭代器
但是一次只接受一个原始字符,没有跳过


有关更多信息,请参阅。

您可以将
count
istreambuf\u迭代器一起使用:

ifstream fs("input.txt");

int num_spaces = count(istreambuf_iterator<unsigned char>(fs),
                       istreambuf_iterator<unsigned char>(),
                       ' ');
ifstream fs(“input.txt”);
int num_spaces=count(istreambuf_迭代器(fs),
istreambuf_迭代器(),
' ');
编辑

最初我的答案使用的是
istream\u迭代器
,但是作为
@Robᵩ指出它不起作用

istream\u迭代器
将在流上迭代,但采用空白格式并跳过它。上面的示例使用
istream\u迭代器
返回的结果为零,因为迭代器跳过了空白,然后我要求它计算剩余的空间

istreambuf_迭代器
但是一次只接受一个原始字符,没有跳过


更多信息。

你可以在你的问题中包含一个代码片段吗?答案在问题更清楚的时候可以更具体。你可以配置C++流是否应该忽略空白,它被称为“SkiPWS”。,如果我没记错的话。while条件是如此愚蠢,我甚至不知道如何对它进行评论。你甚至
fstream::operator bool()
?@Griwes的意思是你可以在{…}中做你能在你的问题中包含一个代码片段吗?答案在问题清楚的时候可以更具体。你可以配置C++流是否应该忽略空白,如果我没记错的话,它被称为“SkiPWS”。条件非常愚蠢,我甚至不怎么评论它。你甚至还没有<代码> fSt::
?@Griwes的意思是你可以在{…}中做+1用于istreambuf_迭代器技巧。我想知道是否有一些东西可以让你用一个迭代器来完成整个文件:)+1用于istreambuf_迭代器技巧。我想知道是否有一些东西可以让你用一个迭代器完成整个文件:)这不起作用至少你认为它是。事实上,它总是返回零。谢谢你的反馈,@Robᵩ这和你想象的不一样。事实上,它总是返回零。谢谢你的反馈,@Robᵩ