Write a program, called sort_file , which takes a text file as input and produce
ID: 3668241 • Letter: W
Question
Write a program, called sort_file, which takes a text file as input and produces an output file which has all the original lines in alphabetical order.
The program should do the following:
Prompt the user for the names of the input and output files. You can prepare an input file with appropriate unsorted data. A simple file with one word per line will suffice.
Declare and define three functions to perform the following tasks:
Read the data from the input file into an array.
Sort the array using the bubble sort algorithm.
Write the sorted data array to the output file.
Explanation / Answer
#include #include #include // Function for reading unsorted data from a file int read(const char *filename, char* arr[]); // Function for sorting gathered data in array void sort(char* arr[], int N); // Function for writing sorted data to file int write(const char *filename, char* arr[], int N); // Helper function for swapping two words void swap(char** a, char** b); // Helper function for dealocatiing memory used by array void final(char* arr[], int N); int main(int argc, char *argv[]) { char filename[260]; char* arr[1000]; // Suppose there are no more than 1000 lines int N; // Size of filled array // Request for input file name printf("Input file name : "); scanf("%s",filename); // Perform reading file if (-1 == (N = read(filename,arr))){ // If error occure while opening the file printf("File can't be opened. "); system("PAUSE"); return 0; } // Sort the data sort(arr,N); printf("Data sorted successfully. "); // Request for output file name printf("Output file name : "); scanf("%s",filename); // Output sorted data if (-1 == (N = write(filename,arr,N))){ // If error occure while opening the file printf("File can't be opened. "); system("PAUSE"); return 0; } system("PAUSE"); // Deallocate used memory final(arr,N); return 0; } int read(const char *filename, char* arr[]){ char buffer[512]; // Suppose that line can't be longer than 511 symbols int i = 0, len; FILE * infile; // Input file // Open file for reading infile = fopen(filename,"r"); // Check whether file was opened correctly if (!infile) // If not then return error code return -1; // Read data while (!feof(infile)){ // Read one line (word) fscanf(infile,"%s",buffer); // Find the length of this word len = strlen(buffer); // Allocate memory for the word... arr[i] = (char*)malloc(len+1); // And copy the word to array strcpy(arr[i],buffer); // Increase counter ++i; } // Close the file fclose(infile); // Return number of elements of created array return i; } void sort(char* arr[], int N){ int i,cmp; for (--N; N>0; --N) for (i=0; i0) // ...then swap them swap(&arr[i],&arr[i+1]); } } int write(const char *filename, char* arr[], int N){ int i; FILE * outfile; // Output file // Open file for writing outfile = fopen(filename,"w"); // Check whether file was opened correctly if (!outfile) // If not then return error code return -1; // Write data for (i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.