Hey guys I need my program to write the sorted array into an output file in addition to printing it. It prints just fine but it returns an empty output file. What am I missing? I know this is gonna be dumb/easy but Im not seeing it.
Code:
#include<stdio.h>
#define MAX 10
void sort(int a[], int n);
int main()
{
int n;
int list = 0;
int i = 0;
int a[MAX];
FILE *outfile;
outfile = fopen("output_lab3.txt", "w");
for (i = 0; i < MAX; i++)
{
printf("Enter Input Number: ");
scanf("%d", &a[i]);
}
sort(a, sizeof(a) / sizeof(int));
list = 0;
while(list < sizeof(a) / sizeof(a[0]))
{
printf("%d", a[list++]);
fprintf(outfile, "\n", a[list++]);
}
printf("\n");
return 0;
fclose(outfile);
}
int sort(int a[], int n)
{
int x, pass, tmp, swch;
pass = 0;
x = 0;
swch = 1;
while(swch)
{
swch = 0;
x = 1;
while(x < n - pass)
{
if(a[x - 1] > a[x])
{
swch = 1;
tmp = a[x-1];
a[x-1] = a[x];
a[x] = tmp;
}
x++;
}
pass++;
}
}