Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/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++_Logging_Stl_Fstream_Seekg - Fatal编程技术网

C++ 如何在C++;?

C++ 如何在C++;?,c++,logging,stl,fstream,seekg,C++,Logging,Stl,Fstream,Seekg,我正在尝试读取一个正在增长的文件(类似于tail-F的功能),但我的代码肯定有一些问题: string log, logFile("test.log"); size_t p = 0; while(true) { ifstream ifs(logFile.c_str()); ifs.seekg(p); //*1 while(ifs.eof() == false) { getline(ifs, log); cout &

我正在尝试读取一个正在增长的文件(类似于
tail-F
的功能),但我的代码肯定有一些问题:

string   log, logFile("test.log");
size_t   p = 0;

while(true)
{
    ifstream ifs(logFile.c_str());

    ifs.seekg(p);  //*1

    while(ifs.eof() == false)
    {
        getline(ifs, log);

        cout << log << endl;

        p = ifs.tellg();  //*2
    }

    nanosleep(&pause, NULL);
}
字符串日志,日志文件(“test.log”);
尺寸p=0;
while(true)
{
ifstream ifs(logFile.c_str());
如果参见kg(p);//*1
while(ifs.eof()==false)
{
getline(ifs,log);

cout循环不正确,因为当遇到
eof()
时,返回
-1
,并且在调用
getline()
之后没有立即检查
eof()
。将循环更改为:

while (getline(ifs, log))
{
    cout << log << endl;
    p = ifs.tellg();
}

如果确实需要为此关闭并重新打开同一文件

不需要,但您需要从流中清除
eof
状态。以下是已发布代码的更正版本的替代方案:

#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream ifs("test.log");

    if (ifs.is_open())
    {
        std::string line;
        while (true)
        {
            while (std::getline(ifs, line)) std::cout << line << "\n";
            if (!ifs.eof()) break; // Ensure end of read was EOF.
            ifs.clear();

            // You may want a sleep in here to avoid
            // being a CPU hog.
        }
    }

    return 0;
}
#包括
#包括
#包括
int main()
{
标准::ifstream ifs(“test.log”);
if(if.is_open())
{
std::字符串行;
while(true)
{

而(std::getline(ifs,line))std::cout这个方法对我来说非常有效:

#include <string>
#include <chrono>
#include <thread>
#include <fstream>
#include <iostream>

int main(int, char* argv[])
{
    // open file passed in on command line (at end of file)
    std::ifstream ifs(argv[1], std::ios::ate);

    if(!ifs.is_open())
    {
        std::cerr << "ERROR: opening log file: " << argv[1] << '\n';
        return 1;
    }

    // remember file position
    std::ios::streampos gpos = ifs.tellg();

    std::string line;
    bool done = false;

    while(!done)
    {
        // try to read line
        if(!std::getline(ifs, line) || ifs.eof())
        {
            // if we fail, clear stream, return to beginning of line
            ifs.clear();
            ifs.seekg(gpos);

            // and wait to try again
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
            continue;
        }

        // remember the position of the next line in case
        // the next read fails
        gpos = ifs.tellg();

        // process line here
        std::cout << "line: " << line << std::endl;
    }
}
#包括
#包括
#包括
#包括
#包括
int main(int,char*argv[]
{
//打开在命令行上传入的文件(在文件末尾)
std::ifstream-ifs(argv[1],std::ios::ate);
如果(!ifs.is_open())
{

std::cerr此代码适用于我:

struct timespec pause;
pause.tv_sec  = 1;
pause.tv_nsec = 0;

std::ifstream ifs("test.log");
std::streamoff p;

if(ifs.is_open())
{
    std::string line;

    while(true)
    {
        if(ifs.seekg(p))
        {
            while(std::getline(ifs, line))
            {
                std::cout << line << std::endl;
                p = ifs.tellg();
            }
        }

        ifs.clear();

        nanosleep(&pause, NULL);
    }
}
struct timespec暂停;
pause.tv_sec=1;
pause.tv_nsec=0;
标准::ifstream ifs(“test.log”);
std::streamoffp;
if(if.is_open())
{
std::字符串行;
while(true)
{
如果(如果参见kg(p))
{
while(std::getline(ifs,line))
{

std::cout因为这些答案都不管用,所以我想出了一个管用的答案

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string   log, logFile("test.txt");
    std::streamoff   p = 0;
    ifstream ifs(logFile.c_str());

    while(true)
    {

        ifs.seekg(p);  //*1
        while (getline(ifs, log))
        {
            cout << log << endl;
            if(ifs.tellg() == -1) p = p + log.size();
            else p = ifs.tellg();
        }
        ifs.clear();

    }
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串日志,日志文件(“test.txt”);
标准::流量p=0;
ifstream ifs(logFile.c_str());
while(true)
{
如果参见kg(p);//*1
while(getline(ifs,log))
{

我是否注意到,如果我在循环中创建ifstream对象,它会工作,但如果我在循环外部创建它,它不会工作。是否需要关闭并重新打开一个文件来读取添加到其中的内容?@Pietro,您需要通过调用
ifs.clear()来清除
ifstream
的eof状态
在下次尝试读取之前。我认为
tellg()和
seekg()
用这种方法是不必要的。@hmjd,你就是那个人!我不得不下地狱去寻找一个简单问题的简单答案。我还没有看到任何比这更清楚的东西。@hmjd:我刚刚尝试了你的最后一段代码,当我在input test.log文件中添加新行时,它们没有被选中。
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string   log, logFile("test.txt");
    std::streamoff   p = 0;
    ifstream ifs(logFile.c_str());

    while(true)
    {

        ifs.seekg(p);  //*1
        while (getline(ifs, log))
        {
            cout << log << endl;
            if(ifs.tellg() == -1) p = p + log.size();
            else p = ifs.tellg();
        }
        ifs.clear();

    }
}