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++ 如何识别C++;?_C++_File_Input - Fatal编程技术网

C++ 如何识别C++;?

C++ 如何识别C++;?,c++,file,input,C++,File,Input,我正在从文件中读取信息。我需要一个计数器来计算有多少文本填充行。我需要计数器停止,如果有任何空行(即使有文字填充行后,该空行) 我该怎么做?因为我不太确定如何识别一个空行来停止计数器。在C++ 11中, 可以使用, std::isblank 如果使用的是std::getline,则可以通过检查刚才读取的std::string是否为空来检测空行 std::ifstream stream; stream.open("file.txt"); std::string text; while(std::

我正在从文件中读取信息。我需要一个计数器来计算有多少文本填充行。我需要计数器停止,如果有任何空行(即使有文字填充行后,该空行)

我该怎么做?因为我不太确定如何识别一个空行来停止计数器。在C++ 11中,

可以使用,

std::isblank

如果使用的是
std::getline
,则可以通过检查刚才读取的
std::string
是否为空来检测空行

std::ifstream stream;
stream.open("file.txt");
std::string text;
while(std::getline(stream,text))
    if(!text.size())
        std::cout << "empty" << std::endl;
std::ifstream;
stream.open(“file.txt”);
std::字符串文本;
while(std::getline(流、文本))
如果(!text.size())

std::cout我建议对它使用
std::getline

#include <string>
#include <iostream>

int main()
{
    unsigned counter = 0;
    std::string line;

    while (std::getline(std::cin, line) && line != "")
        ++counter;

    std::cout << counter << std::endl;
    return 0;
}
#包括
#包括
int main()
{
无符号计数器=0;
std::字符串行;
while(std::getline(std::cin,line)&&line!=“”)
++计数器;

std::cout在一个循环中,将所有行逐个读入一个
字符串
变量。您可以使用
std::getline
函数来实现这一点

每次将一行读入该变量后,检查它的
长度
。如果它为零,则该行为空,在这种情况下,
中断
循环

但是,检查空行(如)并不总是正确的。如果您确定这些行将是空的,那么就可以了。但是如果您的“空”行可以包含空格

123 2 312 3213
12 3123 123
              // <---  Here are SPACEs. Is it "empty"?
123 123 213
123 21312 3
123231213
12 3123 123

//没有错误检查,没有保护,只是一个简单的例子……它没有经过测试,但你得到了要点

#incldue <iostream>
#include <string>

using namespace std;

int main()
{
  string str = "";
  int blank = 0, text = 0;
  ifstream myfile;
  myfile.open("pathToFile");
  while(getline(myfile,str))
  {
    if(str == "")
    {
      ++blank;
    }
    else
    {
      ++text;
    }
  }
  cout << "Blank: " << blank << "\t" << "With text: " << text;
  return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串str=“”;
int blank=0,text=0;
ifstreammyfile;
myfile.open(“pathToFile”);
while(getline(myfile,str))
{
如果(str==“”)
{
++空白;
}
其他的
{
++文本;
}
}

cout只需检查字符串长度并使用行计数器。当字符串长度为零(即字符串为空)时,打印行计数器。提供示例代码供您参考:

// Reading a text file

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
    string line;
    ifstream myfile ("example.txt");
    int i = 0;
    if (myfile.is_open())
    {
        while (getline (myfile, line))
        {
            i++;
            // cout << line << '\n';
            if (line.length() == 0)
                break;
        }
        cout << "blank line found at line no. " << i << "\n";
        myfile.close();
    }
    else
        cout << "Unable to open file";

     return 0;
}
//读取文本文件
#包括
#包括
#包括
使用名称空间std;
int main(){
弦线;
ifstream myfile(“example.txt”);
int i=0;
如果(myfile.is_open())
{
while(getline(myfile,line))
{
i++;

//请问您是如何读取文件的?我会查找回车符或换行符(例如
\r
\r\n
或只是
\n
)。请注意:如果您将其作为文本阅读,您就不必关心
\r
\r\n
\n
之间的差异,就像@GIJoe所建议的那样。这一切都将
\n
出现在您的程序中。这个问题似乎与主题无关,因为OP没有显示他们自己所做的任何努力。@πῥεῖ 我建议阅读。说清楚一点,我也不喜欢它们,但这不是关闭的直接原因。如果需要检测只有空白的行,可以修改while循环,如下所示:while(std::getline(std::cin,line)){If(std::string::npos==line.find_first_not_of(“\t”)++counter;}
// Reading a text file

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
    string line;
    ifstream myfile ("example.txt");
    int i = 0;
    if (myfile.is_open())
    {
        while (getline (myfile, line))
        {
            i++;
            // cout << line << '\n';
            if (line.length() == 0)
                break;
        }
        cout << "blank line found at line no. " << i << "\n";
        myfile.close();
    }
    else
        cout << "Unable to open file";

     return 0;
}