Write a C program not C not C that reads numbers from a text
Write a C program, not C++, not C#, that reads numbers from a text file, sorts the numbers in ascending order, and finally writes the sorted numbers in another file. Text document to be read from, has different numbers on each line, like a column. Any help would be greatly appreciated.
Solution
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
FILE *f1, *f2, *f3;
int number, i,n;
int arr[20];
printf(\"enter number of data elements\ \");
scanf(\"%d\", &n);
printf(\"create Contents of file\ please enter data to the file:\ \");
f1 = fopen(\"contents\", \"w\"); /* Create DATA file */
i=0;
while(i < n){
scanf(\"%d\", &number);
putw(number,f1);
i++;
}
fclose(f1);
f1 = fopen(\"contents\", \"r\");
f2 = fopen(\"result\",\"w\");
i=0;
while(i<n)
{
scanf(\"%d\",&arr[i]);
i++;
}
int j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(arr[i] >= arr[j]){
int temp = arr[i];
arr[i]= arr[j];
arr[j] = temp;
}
}
}
printf(\"\ \ Contents of the file\ \ \");
for(i=0;i<n;i++)
printf(\"%d\\t\",arr[i]);
return 0;
}
