从文件中读取整数并存储在数组中

Reading integers from a file and storing in an array

本文关键字:存储 数组 整数 文件 读取      更新时间:2023-10-16

我目前正在尝试了解从文件中读取数据的一般情况,但是在这种情况下,我正在尝试从txt文件中读取7个整数并将它们存储在数组中。到目前为止,我拥有的代码如下所示。

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
    int arr[7];
    ifstream File;
    File.open("example.txt");
    for (int a = 0; a < 7; a++)
    {
        File >> arr[a];
    }
    for (int i = 0; i < 7; i++)
    {
        cout << arr[i] << " ";
    }
}

我从调试器获得的输出如下所示。我认为这是因为它根本无法打开文件,所以我查找了它并发现 txt 文件应该放在工作目录中。我不确定这到底是什么意思,所以我实际上把它放在项目文件夹的每个文件夹中,但我仍然收到同样的错误。提前感谢任何帮助!

'Project2.exe' (Win32): Loaded 'C:UsersKoolaid LipsDocumentsVisual Studio 2013ProjectsProject2DebugProject2.exe'. Symbols loaded.
'Project2.exe' (Win32): Loaded 'C:WindowsSysWOW64ntdll.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:WindowsSysWOW64kernel32.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:WindowsSysWOW64KernelBase.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcp120d.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcr120d.dll'. Cannot find or open the PDB file.
The program '[6584] Project2.exe' has exited with code 0 (0x0).

我运行了您的代码,结果在 Dev-C++ 编译器中还可以。也许您可以更改代码的编译器。这是PDB文件信息,似乎您可以忽略错误消息。

程序数据库 (PDB) 文件包含调试和项目状态信息,这些信息允许对程序的调试配置进行增量链接。使用/ZI 或/Zi 编译 C/C++ 程序或使用/debug 编译 Visual Basic/C#/JScript .NET 程序时,将创建 PDB 文件。