C++ 为什么这个文件没有读入我的数组?

C++ 为什么这个文件没有读入我的数组?,c++,arrays,io,C++,Arrays,Io,有人能告诉我为什么我的阵列没有填满我试图从数据文件中获取的信息吗?当我输出数组时,它只会给我垃圾。提前谢谢 #include <iostream> #include <fstream> using namespace std; // function main begins program execution int main() { /* input correct answers file */ const int ARRAY_SIZE =

有人能告诉我为什么我的阵列没有填满我试图从数据文件中获取的信息吗?当我输出数组时,它只会给我垃圾。提前谢谢

#include <iostream>
#include <fstream>

using namespace std;

// function main begins program execution
int main()
{
    /* input correct answers file */    
    const int ARRAY_SIZE = 20;      // Array size
    int correctAnswers[ARRAY_SIZE]; // Array to hold correct answers
    int count = 0;                  // Loop counter variable
    ifstream inputFile;             // Input file stream object

    // open the file
    inputFile.open("c:\\correctanswers.txt");

    // read the numbers from the file into the array
    while (count < ARRAY_SIZE)
    {
        inputFile >> correctAnswers[count];
        count++;
    }

    // close the file
    inputFile.close();

    // display the correct answers
    cout << "The correct answers are: ";
    for (int index = 0; index < count; index++)
        cout << correctAnswers[index] << " ";
    cout << endl;
    system ("pause");

    return 0;
}
#包括
#包括
使用名称空间std;
//函数main开始执行程序
int main()
{
/*输入正确答案文件*/
常量int数组_SIZE=20;//数组大小
int correctAnswers[ARRAY_SIZE];//保存正确答案的数组
int count=0;//循环计数器变量
ifstream inputFile;//输入文件流对象
//打开文件
打开(“c:\\correctanks.txt”);
//将文件中的数字读入数组
while(计数<数组大小)
{
输入文件>>正确答案[计数];
计数++;
}
//关闭文件
inputFile.close();
//显示正确答案

coutcorrectAnswers是一个int数组,您试图将来自文件的字符串复制到该数组中。我建议您使用辅助变量存储从文件中获取的行,然后根据需要对其进行操作。此外,您可能希望确保不超过文件结尾限制…

您永远不会检查流是否为I我首先检查流是否能够打开给定的参数:if(inputFile.is\u open)

您是否正在Windows/Linux中尝试?错误消息是什么?文件的内容是什么?文件correctanks.txt是否存在?您没有对输入流进行错误检查。我猜该文件无法打开,或者不包含整数。在Windows中尝试。它不会给我错误消息;它会编译并运行,但在执行cout命令时,它会将数组中的数字显示为一组随机数。哦,天哪!Duh!该文件不包含整数。谢谢。::embarassed::