>计数; 整数[计数]; 对于(int a=0;a>数字[a]; 当你这样做的时候 cout << numbers << endl;,c++,arrays,file,ifstream,C++,Arrays,File,Ifstream" /> >计数; 整数[计数]; 对于(int a=0;a>数字[a]; 当你这样做的时候 cout << numbers << endl;,c++,arrays,file,ifstream,C++,Arrays,File,Ifstream" />

使用数组从文件打印整数 我刚开始学习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; } #包括 #包括 #包括 #包括 int main(){ 使用名称空间std; 整数计数; 河流充填; infle.open(“unsorted.txt”); 填充>>计数; 整数[计数]; 对于(int a=0;a>数字[a]; 当你这样做的时候 cout << numbers << endl;

使用数组从文件打印整数 我刚开始学习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; } #包括 #包括 #包括 #包括 int main(){ 使用名称空间std; 整数计数; 河流充填; infle.open(“unsorted.txt”); 填充>>计数; 整数[计数]; 对于(int a=0;a>数字[a]; 当你这样做的时候 cout << numbers << endl;,c++,arrays,file,ifstream,C++,Arrays,File,Ifstream,这也解决了可变长度数组的问题(因为没有任何数组)。如果您打算使用count variable来计算文件大小或其他内容,这就是代码出错的地方。您不能像尝试那样计算文件的长度 while( getline ( InFile, line ) ) { count += line.length(); } 也许,像这样试试!!! 如果你使用 InFile>>count; 它会尝试将所有字符串从Fipe流存储到计数,这不是故意的。可变长度数组不是标准C++,而是使用。嘿,谢谢帮助!这是我第一次

这也解决了可变长度数组的问题(因为没有任何数组)。

如果您打算使用count variable来计算文件大小或其他内容,这就是代码出错的地方。您不能像尝试那样计算文件的长度

while( getline ( InFile, line ) )
{
count += line.length();
}
也许,像这样试试!!! 如果你使用

InFile>>count;

它会尝试将所有字符串从Fipe流存储到计数,这不是故意的。

可变长度数组不是标准C++,而是使用。嘿,谢谢帮助!这是我第一次使用这种语言,所以我非常感谢你帮助我!
while( getline ( InFile, line ) )
{
count += line.length();
}
InFile>>count;