对于旧的(C类)FSCANF方法,现代等价(C++)风格是什么?

What is the best option if I want to "upgrade" old C-code to newer C++ when reading a file with a semicolon delimiter:

/* reading in from file C-like: */
fscanf(tFile, "%d", &mypost.nr); /*delimiter ; */
fscanf(tFile, " ;%[^;];", mypost.aftername);/* delimiter ; */
fscanf(tFile, " %[^;]", mypost.forename);   /*delimiter ; */
fscanf(tFile, " ;%[^;];", mypost.dept);/*delimiter ; */
fscanf(tFile, " %[^;];", mypost.position);/* delimiter ; */
fscanf(tFile, "%d", &mypost.nr2);

//eqivalent best C++ method achieving the same thing?

#0

You could overload the right-shift operator on istream for your struct, so:

std::istream& operator>>(std::istream& is, mypost_struct& mps) {
    is >> mps.nr;
    is.ignore(1, ';');
    is.getline(mps.forename, 255, ';');
    is.getline(mps.aftername, 255, ';');
    is >> mps.dept;
    is.ignore(1, ';');
    is >> mps.position;
    is.ignore(1, ';');
    is >> mps.nr2;

    return is;
}

Subsequently, input is as simple as is >> mypost;, where is is the file that you have opened.

Edit: @UncleBens Thanks for pointing this out, I had forgotten to take spaces in account. I have updated the answer, assuming that forename and aftername are likely to contain spaces. And there was this rather embarrasing bit about the delimiters being double-quoted...

I just checked it using a struct definition as under:

struct mypost_struct {
    int nr;
    char forename[255], aftername[255];
    int dept, position, nr2;
};

.. and the result was as expected.

#1

As @susmits says, but you can also use the returned stream as a conditional, like:

if (is >> mps.nr && is.ignore(1, ";") && is >> mps.aftername && ...) {
   // all is well ...
} else {
   // bad input format
}

or even:

if (is >> mps.nr >> ignore(";") >> mps.aftername >> ...) {
    // all is well ...
} else {
    // bad input format
}

#2

What is the best option if I want to "upgrade" old C-code to newer C++...?

IMHO, the best way to do this would be to read the file line-by-line and use regular expressions for parsing.

推荐文章

用于Amazon简单通知服务的PHP库

用于Amazon简单通知服务的PHP库

推荐文章

从web服务调用中检索信息

从web服务调用中检索信息

推荐文章

使用UITabBarController时强制从按钮更改视图

使用UITabBarController时强制从按钮更改视图

推荐文章

popViewControllerAnimated from back按钮

popViewControllerAnimated from back按钮

推荐文章

Internet Explorer不显示gif文件

Internet Explorer不显示gif文件

推荐文章

如何减少简单的cpp应用程序大小?(与RAD Studio 2010 cpp builder一起编译)

如何减少简单的cpp应用程序大小?(与RAD Studio 2010 cpp builder一起编译)

推荐文章

由于矩阵维数的原因,MATLAB不会提取第一行和第一列

由于矩阵维数的原因,MATLAB不会提取第一行和第一列

推荐文章

Python:对“imp”Py“InitModule4”的未定义引用

Python:对“imp”Py“InitModule4”的未定义引用

推荐文章

测试web应用程序的最佳实践,涉及域名和与外部服务的集成(如Amazon S3)

测试web应用程序的最佳实践,涉及域名和与外部服务的集成(如Amazon S3)

推荐文章

这个2D数组初始化是个坏主意吗?

这个2D数组初始化是个坏主意吗?

推荐文章

如何在基于验证的ActionLink之前给用户确认消息

如何在基于验证的ActionLink之前给用户确认消息

推荐文章

访问冲突

访问冲突

推荐文章

有没有办法在浏览器中显示三维曲面图?

有没有办法在浏览器中显示三维曲面图?

推荐文章

JQuery语法问题?

JQuery语法问题?

推荐文章

Java2d:为线条设置渐变

Java2d:为线条设置渐变

推荐文章

CODENITENER错误日志显示了404个不存在的图像目录……需要担心吗?

CODENITENER错误日志显示了404个不存在的图像目录……需要担心吗?