使用一个 ifstream 变量读取多个文件

发布于 2024-12-15 18:47:19 字数 534 浏览 6 评论 0原文

可能的重复:
C++ 我可以重用fstream来打开和写入多个文件吗文件?

为什么不能使用一个 ifstream 变量来打开一个文件,读取它,然后关闭它,然后打开另一个文件,读取和关闭等等? 这在代码中看起来如何(假设每个文件里面都有一个整数):

int k, l;  
ifstream input1;  
input1.open("File1.txt");  
input1 >> k;  
input1.close();  
input1.open("File2.txt");  
input1 >> l;  
input1.close(); 

我解决问题的唯一方法是创建另一个 ifstream 变量。

Possible Duplicate:
C++ can I reuse fstream to open and write multiple files?

why is it not possible to use one ifstream variable for opening one file, reading it, then closing it and, after that, opening another file, reading and closing, etc?
How would that look in code (let's just say each file has an integer inside):

int k, l;  
ifstream input1;  
input1.open("File1.txt");  
input1 >> k;  
input1.close();  
input1.open("File2.txt");  
input1 >> l;  
input1.close(); 

the only way I solved the problem was creating another ifstream variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凑诗 2024-12-22 18:47:19

您可以使用相同的变量,在重用之前需要调用 .clear() 来清除对象的标志:

int k,l;
ifstream input1;  
input1.open("File1.txt");  
input1 >> k;  
input1.close();  
input1.clear();
input1.open("File2.txt");  
input1 >> l;  
input1.close();
input1.clear();

但我建议您不要重用它们。如果您不想同时存在多个变量,则可以将每个变量保留在其自己的范围内:

int k,l;

{
    std::ifstream input1("File1.txt");  
    input1 >> k;
}
{
    std::ifstream input1("File2.txt");  
    input1 >> l;  
}

You can use the same variable, you need to call .clear() to clear the object's flags before you reuse it:

int k,l;
ifstream input1;  
input1.open("File1.txt");  
input1 >> k;  
input1.close();  
input1.clear();
input1.open("File2.txt");  
input1 >> l;  
input1.close();
input1.clear();

But I recommend instead you don't reuse them. If you don't want to have more than one variable around at once, you can keep each one in its own scope:

int k,l;

{
    std::ifstream input1("File1.txt");  
    input1 >> k;
}
{
    std::ifstream input1("File2.txt");  
    input1 >> l;  
}
放手` 2024-12-22 18:47:19

ifstream 对象上的 close() 之后调用 clear()

Invoke clear() after close() on ifstream object

星軌x 2024-12-22 18:47:19

问题的原始代码效果很好:

#include <fstream>
using namespace std;
int main()
{
    int k, l;  
    ifstream input1;  
    input1.open("file1.txt");
    input1 >> k;
    input1.close();  
    input1.open("file2.txt");  
    input1 >> l;  
    input1.close(); 
}

注意,来自 C++0X (C ++11) 上,无需调用 basic_ifstream::clear() 因为标准指定(请参阅 C++0x/27.9.1.9)成功 basic_ifstream::open() 应调用basic_ifstream::clear()。

The original code from the question works well:

#include <fstream>
using namespace std;
int main()
{
    int k, l;  
    ifstream input1;  
    input1.open("file1.txt");
    input1 >> k;
    input1.close();  
    input1.open("file2.txt");  
    input1 >> l;  
    input1.close(); 
}

Note, from C++0X (C++11) on, there is no need to call basic_ifstream::clear() because the standard specifies (see C++0x/27.9.1.9) successful basic_ifstream::open() shall call basic_ifstream::clear().

凝望流年 2024-12-22 18:47:19

虽然完全可以按照您的建议进行操作(尽管如果提取失败,您可能需要清除流的错误标志),但它并不是非常优雅。在 C++ 中,您应该在需要时随意创建新对象,并在完成后丢弃它们。也许编写此代码的更系统的方法是在本地循环范围内:

int result;

while (true)
{
  std::ifstream infile(get_random_file_name());
  if (infile >> n) { break; }
  // "infile" gets destroyed at the end of the scope
}

std::cout << "We found a good file, and it contains: " << n << std::endl;

您当然可以将循环逻辑替换为更适合您的情况的任何其他内容。例如,根据零一多规则,您可能在某个地方有一个包含候选文件名的容器;或者您可能会循环遍历命令行参数。

While it's entirely possible to do what you suggest (though you may need to clear the stream's error flags if the extraction failed), it's not terribly elegant. In C++, you should feel free to make new objects when you need them, and discard them when you're done. Perhaps a more systematic way to write this code is in a local loop scope:

int result;

while (true)
{
  std::ifstream infile(get_random_file_name());
  if (infile >> n) { break; }
  // "infile" gets destroyed at the end of the scope
}

std::cout << "We found a good file, and it contains: " << n << std::endl;

You can of course replace the loop logic by anything else that's more appropriate to your situation. For example, according to the Zero-One-Many rule, you might have a container somewhere that contains your candidate filenames; or you could be looping over command-line arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文