我一直在玩这个,但我没有得到任何帮助。我正在尝试从txt文件读取整数列表到一个数组(1,2,3,...)。我知道将被读取的整数的数量是100,但我似乎无法填充数组。每次我运行代码本身时,它仅为所有100个整数存储值0。有什么想法吗?从文本文件读入数组
//Reads the data from the text file
void readData(){
ifstream inputFile;
inputFile.open("data.txt");
if (!inputFile){
//error handling
cout << "File can't be read!";
}
else{
int a;
while (inputFile >> a){
int numbers;
//Should loop through entire file, adding the index to the array
for(int i=0; i<numbers; i++){
DataFromFile [i] = {numbers};
}
}
}
}
谢谢您的回答。第一种方法工作,现在我的程序已经启动并运行:) – user3061066 2014-11-24 00:54:10