A practical program that reads data from a text file to sort

Source: Internet
Author: User

Program Description

This is a very reliable program, the process of the error-checking ability is very powerful. The program contains a variety of techniques such as file manipulation, merge sorting, and string input.

The function of the program is to read from an external text file that includes int data, and then save it to an internal temporary array, sorting the array and outputting it to the specified file as text. Because the data is of type int, there is no serious problem of loss precision.

Normal operation requirements:

The source file that includes the data cannot include any other characters except numbers and whitespace characters (spaces, tabs, newline characters), the source file must start with a numeric character, and the source file's data count is correct. Also ensure that the file name is valid.

Full code

Warning: All rights reserved, for reference !

#include <stdio.h>#include<stdlib.h>#include<conio.h>/*============================= made in: qq:1729403632===============================*/#defineST 64//String SizevoidMergeSort (int*,int);void_mergesort (int*,int,int,int*);voidMergeint*,int,int,int,int*);Char* S_gets (Char*,int);intMainintargcChar*argv[]) {FILE* Sor, * dest;//Sor source file dest destination file    int* PTR;//Temporary Array    intI, N;//n Indicates the number of elements    CharFNAME[ST];//Temporary storage stringprintf ("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) );//Apply Dynamic Array//////    if(ptr = =NULL) {fprintf (stderr,"FAIL to ASK for MEMORY space\n");    Exit (Exit_failure); } printf ("Please 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 that contains the data    if(Sor = =NULL) {fprintf (stderr,"Fail to open the source file\n");    Exit (Exit_failure); }     for(i =0; I < n; i++)//getting 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); //Sortprintf ("Please enter the file name 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 destination 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 target file        if(fprintf (dest,"%d\t", Ptr[i]) <0) {fprintf (stderr,"Fail to save the data\n");        Exit (Exit_failure); }        if(((i +1) %Ten) ==0){//If you write 10, wrap it.            if(fprintf (dest,"\ n") <0) {fprintf (stderr,"Fail to save the data\n");            Exit (Exit_failure); }        }    }    if(Fclose (sor)! =0){//Close Source filefprintf (stderr,"Fail to close the source file\n");    Exit (Exit_failure); }    if(Fclose (dest)! =0){//Close the target filefprintf (stderr,"Fail to close the destination file\n");    Exit (Exit_failure); }     Free(PTR);//Freeing Memoryprintf ("completed successfully! \ n Please press any key to continue ^ ^\b\b");    Getch (); return 0;}voidMergeSort (int* AR,intsize) {    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,intStartintEndint*temp) {    if(Start <end) {        intMid = (start + end)/2;    _mergesort (AR, start, Mid, temp); //left dial hand array sorting_mergesort (AR, Mid +1, end, temp);//right Sub-array orderingMerge (AR, start, Mid, end, temp);//merging sub-arrays    }}voidMergeint* AR,intPintQintRint*temp) {    inti = p, j = q +1, k =0;  while(I <= q && J <=R) {        if(Ar[i] <Ar[j]) temp[k+ +] = ar[i++]; Elsetemp[k+ +] = ar[j++]; }     while(I <= Q)//if the sequence [i...q] exists, appendtemp[k++] = ar[i++];  while(J <= R)//if the sequence [J...R] exists, appendtemp[k++] = ar[j++];  for(k =0; K <= (R-P); k++) Ar[p+ K] =temp[k];}Char* S_gets (Char* St,intsize) {    Char*re; inti =0; Re=fgets (St, size, stdin); if(re) { while(St[i]! ='\ n'&& St[i]! =' /')//If there is no end to the input stringi++;//Increment        if(St[i] = ='\ n')//if the last character of the string is ' \ n 'St[i] =' /';//Turn it into ' a '        Else //Otherwise, some of the characters in the buffer that are outside the read range are not read             while(GetChar ()! ='\ n')//finish reading these characters (empty buffer)                Continue; }    returnre;}
main.cRun results

Data.txt:

Obj.txt:

A practical program that reads data from a text file to sort

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.