简体   繁体   English

在排序程序中从文本文件读取到数组

[英]Reading from a text file to an array in a sorting program

I am creating a sorting program that contains Selection, Insertion, and Bubble Sorts.我正在创建一个包含选择、插入和冒泡排序的排序程序。 So far, I am successful enough to create the program and input the value from the user.到目前为止,我已经成功地创建了程序并输入了用户的值。

But I want to use a text file to input and store the values to the array instead of user entering the value.但我想使用文本文件来输入值并将其存储到数组中,而不是用户输入值。 I tried multiple times putting the input from text file feature in the program but got no success.我多次尝试将文本文件功能的输入放入程序中,但没有成功。

Please have a look at my code in the links below.请在下面的链接中查看我的代码。 I would really appreciate if you guys can help me on this.如果你们能在这方面帮助我,我将不胜感激。

Thanks谢谢

In this Method: void accept(int Arr[], int s)在这个方法中: void accept(int Arr[], int s)

You are reading to the array using capital I as index.您正在使用大写 I 作为索引读取数组。 But that variable seems to not be declared.但是那个变量似乎没有被声明。

int Index = 0;
While(!file.eof()) {
    file >> Arr[Index];
    Index++;
}

Beside that, you may want to pass the length of the Array to the accept-Funktion, to avoid to read to it while exceeding it's length.除此之外,您可能希望将 Array 的长度传递给 accept-Funktion,以避免在超过它的长度时读取它。 Also you may want to close the file input stream explicically after you finished reading, just for good code style and readability.此外,您可能希望在完成阅读后明确关闭文件输入流,只是为了获得良好的代码风格和可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM