A practical program for reading data from text files and sorting text files
Program description
This is a very reliable program, which has a very powerful ability to identify errors. Programs include file operations, Merge Sorting, string input, and other technologies.
The function of the program is to read a text file containing int data from the outside, and save it to an internal temporary array. After sorting the array, output to the specified file in text format. There is no serious loss of precision due to data of the int type.
Normal operation requirements:
The source file containing data cannot contain any other characters except numbers and spaces (spaces, tabs, line breaks). The source file must start with a number character, ensure that the data count of the source file is correct. Ensure that the file name is valid.
Complete code
Warning: All rights reserved for your reference!
# Include <stdio. h> # include <stdlib. h> # include <conio. h>/* =: aug 16,201 6 by QQ: 1729403632 =================================== */# define ST 64 // string size void mergesort (int *, int); void _ mergesort (int *, int *); void merge (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 indicates the number of elements. char fname [ST]; // temporary storage string printf ("Enter the number of elements :"); while (scanf ("% d", & n )! = 1 | n <= 0) {printf ("input error. Please enter it again! \ N "); while (getchar ()! = '\ N') continue;} while (getchar ()! = '\ N') continue; ptr = (int *) malloc (size_t) n * sizeof (int )); // apply FOR a dynamic array //// if (ptr = NULL) {fprintf (stderr, "fail to ask for memory space \ n "); exit (EXIT_FAILURE);} printf ("Enter the original file name:"); 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 data if (sor = NULL) {fprintf (stderr, "Fail to open the source file \ n"); exit (E XIT_FAILURE) ;}for (I = 0; I <n; I ++) // get data to a dynamic array if (fscanf (sor, "% d ", & ptr [I])! = 1) {fprintf (stderr, "Fail to get the data \ n"); exit (EXIT_FAILURE);} mergesort (ptr, n ); // sort printf ("enter the name of the file to save the data:"); 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 (des T, "% d \ t", ptr [I]) <0) {fprintf (stderr, "Fail to save the data \ n"); exit (EXIT_FAILURE );} if (I + 1) % 10) = 0) {// if there are 10 full lines, wrap the line if (fprintf (dest, "\ n") <0) {fprintf (stderr, "Fail to save the data \ n"); exit (EXIT_FAILURE) ;}} if (fclose (sor )! = 0) {// close the source file fprintf (stderr, "Fail to close the source file \ n"); exit (EXIT_FAILURE);} if (fclose (dest )! = 0) {// close the target file fprintf (stderr, "Fail to close the destination file \ n"); exit (EXIT_FAILURE);} free (ptr ); // release the memory printf ("completed successfully! \ N press any key to continue ^ \ 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); // sort the left sub-array _ mergesort (ar, mid + 1, end, temp ); // sort the right sub-array merge (ar, start, mid, end, temp); // merge sub-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] Storage In, 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]! = '\ 0') // if the input string does not end with I ++; // incrementing if (st [I] =' \ n ') // if the last character of the string is '\ n' st [I] =' \ 0 '; // change it to '\ 0' else // otherwise, some characters in the buffer that exceed the read range are not read while (getchar ()! = '\ N') // read these characters (clear the buffer) continue;} return re ;}Main. c running result
Data.txt:
Obj.txt: