Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/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
C++ 如何确定输入文件中的一行是否为最后一行?c++;_C++_Algorithm_If Statement_Input_Eof - Fatal编程技术网

C++ 如何确定输入文件中的一行是否为最后一行?c++;

C++ 如何确定输入文件中的一行是否为最后一行?c++;,c++,algorithm,if-statement,input,eof,C++,Algorithm,If Statement,Input,Eof,我编写了一个程序来检查.cpp文件中是否有平衡的花括号。程序运行正常,找到语法错误,显示出现问题的行数,然后退出 但是如果错误在输入cpp文件的最后一行,我必须显示不同的错误消息 我试着按照下面的方法实现它,但我认为它是错误的。反正也不行:) else { if(current==inputFile.eof())//这就是我尝试的 { cout这是我能想到的最好的解决办法。可能还有更好的办法 std::ifstream input_file{ "file.txt }; std::vector&l

我编写了一个程序来检查.cpp文件中是否有平衡的花括号。程序运行正常,找到语法错误,显示出现问题的行数,然后退出

但是如果错误在输入cpp文件的最后一行,我必须显示不同的错误消息

我试着按照下面的方法实现它,但我认为它是错误的。反正也不行:)

else
{
if(current==inputFile.eof())//这就是我尝试的
{

cout这是我能想到的最好的解决办法。可能还有更好的办法

std::ifstream input_file{ "file.txt };
std::vector<std::string> contents;

// fill vector with file contents
std::string cline;
while (std::getline(input_file, cline))
    contents.push_back(cline);

// now loop
for (const auto& line : contents) {
    //...
    if (&line == &contents.back()) {
        // do something at the end of file
    }
}
std::ifstream输入_文件{“file.txt};
std::载体内容;
//用文件内容填充向量
std::字符串渐变;
while(std::getline(输入文件,cline))
内容。推回(倾斜);
//现在循环
用于(常量自动和行:内容){
//...
if(&line==&contents.back()){
//在文件末尾做些什么
}
}

如果您不喜欢指针比较,可以使用迭代器版本:)

了解完整的代码会很好。这样,我们可以更好地找到代码的解决方案(当然,制作一个。现在,我们只能猜测您如何读取当前的
,…因此无法提供一个好的解决方案。
eof
标志不会被设置,除非您尝试读取文件的结尾。这应该在中指出。可能会寻找结尾并向后读取,直到命中
\n
,然后是
ftell()
call给你这个职位。无论你何时获得这个职位,你都在最后一行。@raket1111我编辑了这篇文章。@CinCout是的,这是一次拙劣的尝试:)
for(int i = 0; i < line.length(); i++)
        {
            if (line[i] == '{')
            {
                stack.push(current);
            }
            else if(line[i] == '}')
            {
                if (!stack.isEmpty())
                {
                    stack.pop(opening);
                    cout << "Code block: " << opening << " - " << current << "\n";
                }
                else
                {
                    if(current == inputFile.eof())
                    {
                        cout << "Syntax error at the end of the program.";
                    }
                    else
                    {
                        cout << "Syntax error in line: " << current << "\n";
                        errorFound == true;
                    }
                }
            }
std::ifstream input_file{ "file.txt };
std::vector<std::string> contents;

// fill vector with file contents
std::string cline;
while (std::getline(input_file, cline))
    contents.push_back(cline);

// now loop
for (const auto& line : contents) {
    //...
    if (&line == &contents.back()) {
        // do something at the end of file
    }
}