如何在 C++ 中使用 ifstream 打开和读取文件?

发布于 2024-09-11 06:36:47 字数 604 浏览 3 评论 0原文

我想打开一个文件并从中读取一行。文件中只有一行,所以我真的不需要担心循环,尽管为了将来的参考,知道如何读取多行会很好。

int main(int argc, const char* argv[]) {

    // argv[1] holds the file name from the command prompt

    int number = 0; // number must be positive!

    // create input file stream and open file
    ifstream ifs;
    ifs.open(argv[1]);

    if (ifs == NULL) {
        // Unable to open file
        exit(1);
    } else {
        // file opened
        // read file and get number
        ...?
        // done using file, close it
        ifs.close();
    }
}

我该怎么做?另外,就成功打开而言,我是否正确处理打开的文件?

谢谢。

I would like to open a file and read a line from it. There will be only one line in the file so I don't really need to worry about looping, although for future reference it would be nice to know how to read multiple lines.

int main(int argc, const char* argv[]) {

    // argv[1] holds the file name from the command prompt

    int number = 0; // number must be positive!

    // create input file stream and open file
    ifstream ifs;
    ifs.open(argv[1]);

    if (ifs == NULL) {
        // Unable to open file
        exit(1);
    } else {
        // file opened
        // read file and get number
        ...?
        // done using file, close it
        ifs.close();
    }
}

How would I do this? Also, am I handling the file open correctly in terms of successful open?

Thanks.

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

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

发布评论

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

评论(2

偏闹i 2024-09-18 06:36:48

有几件事:

  1. 您可以使用>>流提取运算符读取数字:ifs >> number

  2. 标准库函数 getline 将从文件中读取一行,如果你想要一整行文本。

  3. 要检查文件是否打开,只需编写 if (ifs)if (!ifs)。省略 == NULL

  4. 您不需要在最后显式关闭文件。当 ifs 变量超出范围时,这会自动发生。

修改后的代码:

if (!ifs) {
    // Unable to open file.
} else if (ifs >> number) {
    // Read the number.
} else {
    // Failed to read number.
}

A couple of things:

  1. You can read a number with the >> stream extraction operator: ifs >> number.

  2. The standard library function getline will read a line from a file, if you want a full line of text.

  3. To check if the file opened, just write if (ifs) or if (!ifs). Leave out the == NULL.

  4. You don't need to explicitly close the file at the end. That will happen automatically when the ifs variable goes out of scope.

Revised code:

if (!ifs) {
    // Unable to open file.
} else if (ifs >> number) {
    // Read the number.
} else {
    // Failed to read number.
}
吃兔兔 2024-09-18 06:36:48

对于您在这里所做的事情,很简单:

ifs >> number;

将从流中提取一个数字并将其存储在“number”中。

循环,取决于内容。如果都是数字,类似:

int x = 0;
while (ifs >> numbers[x] && x < MAX_NUMBERS)
{
 ifs >> number[x];
 x++;
}

可以将一系列数字存储在数组中。这是有效的,因为如果提取成功,则提取运算符的副作用为 true,如果失败(由于文件结尾或磁盘错误等),则为 false。

For what you're doing here, simply:

ifs >> number;

Will extract a number from the stream and store it in 'number'.

Looping, depends on the content. If it was all numbers, something like:

int x = 0;
while (ifs >> numbers[x] && x < MAX_NUMBERS)
{
 ifs >> number[x];
 x++;
}

Would work to store a series of numbers in an array. This works because extraction operator's side effect is true if the extraction succeeds, or false if it fails (due to end of file or disk errors, etc.)

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