I am trying to sort three files (I/O) using bubble sort. For this purpose, I write this code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void bubble_Sort_ascending_order(ll n, ll v[]){
ll i,j;
for(i=1; i<=n-1; i++)
{
for(j=1; j<=n-i; j++)
{
if(v[j]>v[j+1])
swap(v[j],v[j+1]);
}
}
}
int bubble_Sort_ascending_input1() {
freopen("input1.txt","r",stdin);
freopen("output_File1.txt","w",stdout);
ll n;
cin >> n;
ll t[n];
for(ll i=1; i<=n; i++)
{
cin >> t[i];
}
bubble_Sort_ascending_order(n,t);
for(ll k=1; k<=n; k++)
{
cout << t[k] << endl;
}
}
int bubble_Sort_ascending_input2() {
freopen("input2.txt","r",stdin);
freopen("output_File2.txt","w",stdout);
ll n;
cin >> n;
ll r[n];
for(ll i=1; i<=n; i++)
{
cin >> r[i];
}
bubble_Sort_ascending_order(n,r);
for(ll k=1; k<=n; k++)
{
cout << r[k] << endl;
}
}
int bubble_Sort_ascending_input3() {
freopen("input3.txt","r",stdin);
freopen("output_File3.txt","w",stdout);
ll n;
cin >> n;
ll v[n];
for(ll i=1; i<=n; i++)
{
cin >> v[i];
}
bubble_Sort_ascending_order(n,v);
for(ll k=1; k<=n; k++)
{
cout <<v[k]<<endl;
}
}
int main(){
bulble_Sort_ascending_input1();
bubble_Sort_ascending_input2();
bubble_Sort_ascending_input3();
}
First input file function output is working properly. Second and third input function gives garbage value in output file two and three.
But I can't seem to locate any solutions to a similar problem. I'm really having trouble with understanding file I/O for some reason. Thanks in advance for the help.