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

Reading integers from a file and storing in an array

我目前正在尝试理解从文件中读取一般数据,但是在这种情况下,我试图从 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:\Users\Koolaid Lips\Documents\Visual Studio 2013\Projects\Project2\Debug\Project2.exe'. Symbols loaded.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
The program '[6584] Project2.exe' has exited with code 0 (0x0).

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

A program database (PDB) file holds debugging and project state information that allows incremental linking of a Debug configuration of your program. A PDB file is created when you compile a C/C++ program with /ZI or /Zi or a Visual Basic/C#/JScript .NET program with /debug.