function Introduction
The function of the program is to read from the outside a text file that includes int data, and then save it to an internal temporary array, sorted by array, and output to the specified file in text form. Because it is an int type of data, there is no serious problem of loss accuracy.
Normal operation requirements:
The source file that includes the data cannot include any other characters except numbers and whitespace characters (spaces, tabs, line breaks), which must begin with numeric characters, to ensure that the source files are counted correctly. Also ensure that the file name is valid.
Run results
Data.txt:
Obj.txt:
Complete code
Warning: All rights reserved, for reference!
#include <stdio.h> #include <stdlib.h> #include <conio.h>/*============================= made in: Aug 16
, 2016 by qq:1729403632 ===============================*/#define ST 64//String size void mergesort (int *, int);
void _mergesort (int *, int, int, int *);
void merge (int *, int, int, int, int *);
char * s_gets (char *, int); int main (int argc, char * argv[]) {file * sor, * dest;//sor source file dest destination file int * ptr;//temporary array int i, n;//n representing number of elements Char FNAME[ST];
Temporary storage string printf ("Please enter the number of elements:"); while (scanf ("%d", &n)!= 1 | | n <= 0) {printf ("Input error, please re-enter!")
\ n ");
while (GetChar ()!= ' \ n ') continue;
while (GetChar ()!= ' \ n ') continue; PTR = (int *) malloc ((size_t) n * sizeof (int));
Request Dynamic Array//////if (ptr = NULL) {fprintf (stderr, "FAIL to ask for MEMORY space\n");
Exit (Exit_failure);
printf ("Please enter the original filename:");
if (S_gets (fname, ST) = = NULL) {fprintf (stderr, "Fail to get a string\n");
Exit (Exit_failure); } sor = fopen (fName, "R");
Open the source file containing the data if (sor = = NULL) {fprintf (stderr, Fail to open the source file\n);
Exit (Exit_failure); for (i = 0; i < n; i++)//Get data to dynamic array if (FSCANF (sor, "%d", &ptr[i))!= 1) {fprintf (stderr, "Fail to G")
ET the data\n ");
Exit (Exit_failure); } mergesort (PTR, n);
Sort printf ("Enter the filename of the data you want to Save:");
if (S_gets (fname, ST) = = NULL) {fprintf (stderr, "Fail to get a string\n");
Exit (Exit_failure); } dest = fopen (fname, "w");
Open the target file if (dest = = NULL) {fprintf (stderr, "Fail to open the destination file\n");
Exit (Exit_failure); for (i = 0; i < n; i++) {//output data to the target file if (fprintf (dest, "%d\t", Ptr[i)) < 0) {fprintf (stderr, "Fail to
Save the data\n ");
Exit (Exit_failure); } if (((i + 1)% 10) = = 0) {//If you write full 10, wrap to line if (fprintf (dest, "\ n") < 0) {fprintf (stderr, "Fail to save
The data\n ");
Exit (Exit_failure); }} if (Fclose (SOR)!= 0) {//close source file Fprintf (stderr, "Fail to close the source file\n");
Exit (Exit_failure);
} if (Fclose (dest)!= 0) {//close target file fprintf (stderr, "Fail to close the destination file\n");
Exit (Exit_failure); Free (PTR); Free memory printf ("completed successfully!")
\ nplease Press any key to continue ^ ^\b\b ");
Getch ();
return 0;
} void mergesort (int * ar, int size) {if (Size > 0) {int * temp; temp = (int *) malloc ((size_t) size * sizeof (int));
if (temp = NULL) {fprintf (stderr, "Fail to ask for MEMORY space\n");
Exit (Exit_failure); } _mergesort (AR, 0, size-1, temp);
Merge sort free (temp);
} void _mergesort (int * ar, int start, int end, int * temp) {if [start < end] {int mid = (start + end)/2; _mergesort (AR, start, Mid, temp); Zoozi array sort _mergesort (AR, mid + 1, end, temp); Right Sub array sort merge (AR, start, Mid, end, temp);
Merge Child Array}} void merge (int * ar, int p, int q, int r, int * temp) {int i = p, j = q + 1, k = 0; while (i <= Q && J <= r) {if (Ar[i] < Ar[j]) temp[k++] = ar[i++];
else temp[k++] = ar[j++];
while (i <= Q)//If the sequence [i...q] exists, append temp[k++] = ar[i++];
while (J <= R)//if the sequence [J...R] exists, append temp[k++] = ar[j++];
for (k = 0; k <= (r-p); k++) Ar[p + K] = temp[k];
char * s_gets (char * st, int size) {char * RE;
int i = 0;
Re = fgets (st, size, stdin); if (re) {while (st[i]!= ' \ n ' && st[i]!= ' i ')//If there is no end to the input string i++; Increment if (st[i] = = ' \ n ')//If the last character of the string is ' \ n ' st[i] = ';
Turn it into ' I ' else//otherwise a part of the buffer that is out of the read range is not read while (GetChar ()!= ' \ n ')//reads the characters out (empty the buffer) continue;
return to re; }
Summarize
The above is the use of C language to read data from the text file after the sorting function of the entire content, read this article, everyone to run their own debugging, I believe it will be helpful to learn C language friends.