【发布时间】:2018-02-12 00:33:28
【问题描述】:
我刚刚开始学习 C++,但在使用程序时遇到了一些问题。它应该对外部文件中的数字进行排序。我已经成功地编写了排序算法,但是在处理外部文件时遇到了麻烦。我只是在一个单独的程序中测试一些东西,以了解 ifstream 之类的东西是如何工作的。一旦我更好地了解了它的工作原理,我应该能够弄清楚如何将它实现到我的程序中。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main() {
using namespace std;
int count;
ifstream InFile;
InFile.open ("unsorted.txt");
InFile >> count;
int numbers[count];
for(int a = 0; a < count; a++)
InFile >> numbers[a];
cout << numbers << endl;
}
目前,此输出为 0x7ffc246c98e0 我不确定为什么会出现这种情况,我只是试图打印我的整数文件。谁能帮助解释我做错了什么?非常感谢。
【问题讨论】:
-
可变长度数组不是标准 C++,请改用
std::vector。