简体   繁体   中英

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)

You are reading to the array using capital I as index. 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. Also you may want to close the file input stream explicically after you finished reading, just for good code style and readability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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