如何从文件中读取时间而不转换 [英] How to read time from a file without converting

查看:159
本文介绍了如何从文件中读取时间而不转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是一般的,但我会用一个具体的例子解释。



假设我需要在两个应用程序之间传递时间。一个简单的方法是让一个应用程序写输出 gettimeofday() tv_sec tv_usec )到一个文件,让其他应用读取它。第二个应用程序需要转换字符串,以获取 timeval 的实例。



如何避免转换?



有没有比简单文件写入/读取更好的方法?

解决方案

假设两个进程都在同一台机器上(或至少在同一架构的机器上),结果 std :: time() (来自 < ctime> )将自纪元以来的秒数,且不需要任何转换:

  std :: time_t seconds_since_epoch = std :: time(NULL); 



免责声明:这不是,您需要 锁定文件 以便在正在写入时进行阅读, etc






更新,以下评论。 b

如果你需要写一个 timeval ,也许最简单的方法是定义<< >> 运算符 timeval ,并将它们作为文本写入并读取到文件担心字节顺序)原样(没有转换):

  std :: ostream& operator<<<(std :: ostream& out,timeval const& tv)
{
return out< tv.tv_sec<< < tv.tv_usec;
}

std :: istream&运算符>>(std :: istream& is,timeval& tv)
{
return is> tv.tv_sec>> tv.tv_usec;
}

这将允许您执行以下操作(忽略并发):

  //作者
{
timeval tv;
gettimeofday(& tv,NULL);
std :: ofstream timefile(filename,std :: ofstream :: trunc);
timefile< tv < std :: endl;
}

//读者
{
timeval tv;
std :: ifstream timefile(filename);
timefile>>电视;
}

如果两个进程同时运行,则需要锁定文件。以下是使用 Boost a>:

  //作者
{
timeval tv;
gettimeofday(& tv,NULL);
file_lock lock(filename);

scoped_lock< file_lock> lock_the_file(lock);

std :: ofstream timefile(filename,std :: ofstream :: trunc);
timefile< tv < std :: endl;
timefile.flush();
}

//读者
{
timeval tv;
file_lock lock(filename);

sharable_lock< file_lock> lock_the_file(lock);

std :: ifstream timefile(filename);
timefile>>电视;

std :: cout<< tv < std :: endl;
}

...我省略了 例外 处理(当文件不存在时);您需要将其添加到任何具有生产价值的代码中。


My question is general, but I will explain it using a specific example.

Suppose I need to communicate time between two applications. A simple way is to have one application write the output of gettimeofday() (tv_sec and tv_usec) to a file and let the other app read it. The second app needs to 'convert' the strings in order to get an instance of timeval.

Is there any way to avoid the conversion?

Is there a better way to do this than simple file write/read?

解决方案

Assuming both processes are on the same machine (or at least on machines of the same architecture), the results of std::time() (from <ctime>) will be seconds since the Epoch, and will not need any conversion:

std::time_t seconds_since_epoch = std::time(NULL);

Disclaimer: This is not the best method of and you will need to lock the file for reading while it is being written, etc. Just answering the question.


Update, following comment.

If you need to write a timeval, perhaps the easiest way is to define << and >> operators for timeval and write and read these as text to the file (no need to worry about byte-ordering) as-is (with no conversion):

std::ostream& operator <<(std::ostream& out, timeval const& tv)
{
    return out << tv.tv_sec << " " << tv.tv_usec;
}

std::istream& operator >>(std::istream& is, timeval& tv)
{
    return is >> tv.tv_sec >> tv.tv_usec;
}

This will allow you to do the following (ignoring concurrency):

// Writer
{
    timeval tv;
    gettimeofday(&tv, NULL);
    std::ofstream timefile(filename, std::ofstream::trunc);
    timefile << tv << std::endl;
}

// Reader
{
    timeval tv;
    std::ifstream timefile(filename);
    timefile >> tv;
}

If both process are running concurrently, you'll need to lock the file. Here's an example using Boost:

// Writer
{
    timeval tv;
    gettimeofday(&tv, NULL);
    file_lock lock(filename);

    scoped_lock<file_lock> lock_the_file(lock);

    std::ofstream timefile(filename, std::ofstream::trunc);
    timefile << tv << std::endl;
    timefile.flush();
}

// Reader
{
    timeval tv;
    file_lock lock(filename);

    sharable_lock<file_lock> lock_the_file(lock);

    std::ifstream timefile(filename);
    timefile >> tv;

    std::cout << tv << std::endl;
}

...I've omitted the exception handling (when the file does not exist) for clarity; you'd need to add this to any production-worthy code.

这篇关于如何从文件中读取时间而不转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆