Reading integers from file into array

closed account (GhqjLyTq)
So I'm trying to write a program that will read some integers from a text file, sort them, then write the sorted array into another text file. It is sorting and writing them into a file, but it won't read them from a file. I can't use getline() since I'm using integers. Here's my code, anyone know how to get it to read integers from a file into an array, instead of the array being initialized with the integers?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <Windows.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
void selectionSort(int arr[], int size); 
void swap(int& x, int& y); 
int main() 
{ 
     int numbers[] = { 13, 5, 1, 7, 9, 11, 3, 17, 19, 15, 234 }; 
     int maximum = sizeof( numbers ) / sizeof( numbers[0] );
     int k; 
     selectionSort(numbers, maximum); 
     ofstream output;
     output.open("sorted.txt");
     for (k = 0; k < maximum; k++) 
     output << numbers[k] << "\n"; 
     output.close();
     system("sorted.txt"); 
     return 0; 
}
void selectionSort(int arr[], int size) 
{ 
     int indexOfMin, pass, j; 
     for (pass = 0; pass < size - 1; pass++) 
     { 
           indexOfMin = pass; 
           for (j = pass + 1; j < size; j++) 
                 if (arr[j] < arr[indexOfMin]) 
                       indexOfMin = j; 
           swap(arr[pass], arr[indexOfMin]); 
     } 
}
void swap(int& x, int& y) 
{ 
     int temp; 
     temp = x; 
     x = y; 
     y = temp; 
}
Have you tried to actually code reading the numbers from the file? What kinds of problems are you having when you attempt to do that?
Topic archived. No new replies allowed.