-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread&writeFile.cpp
More file actions
54 lines (49 loc) · 1.72 KB
/
read&writeFile.cpp
File metadata and controls
54 lines (49 loc) · 1.72 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
/*
* readFile.cpp
*/
/*
? reads the numbers from the number.txt file
? and writes them back in order
? to the sorted_numbers.txt file
*/
#include <iostream>
#include <fstream>
using namespace std;
void bubbleSort(int * a, int n){
bool done = true;
do {
done = true;
for (int i = 0; i < n-1; i++){
if (a[i] > a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
done = false;
}
}
} while (!done);// ? repeated until done equals false.
}
int main(int argc, char** argv){
// ? "input file stream", an input stream to a file, to set a pointer to read from the file.
ifstream myfile;// -> FILE * fptr; (in C)
// ? opens the file with a pointer set to read from the file.
myfile.open("numbers.txt"); // -> fptr = fopen("numeri.txt"); (in C)
int n;
myfile >> n;// ? reads the first line to store n, which indicates the number of elements to read;
int * dati = new int[n];
// print in terminal and save to array
for(int i = 0; i < n; i++){
myfile >> dati[i];// ? The extract operator >> reads data from the file by positioning the pointer to the next location in the file.
cout << dati[i] << endl;
}
myfile.close(); // -> fclose(myfile); (in C)
bubbleSort(dati, n); // sort array
ofstream outfile; // ? ofstream, "output file stream", an output stream to a file, to set a pointer to write to the file.
outfile.open("sorted_numbers.txt");// ? opens the file with a pointer set to write to the file.
outfile << n << endl;
for (int i = 0; i < n; i++){
outfile << dati[i] << endl;
}
outfile.close();// ? closes the output file
return 0;
}