Use C to implement a simple Paster-to every programmer who once sought the power of AC

Source: Internet
Author: User

Implementation idea: one input file (input.txt.pdf, two comparison programs (main1.txt,main2.txt), and the other two will redirect the standard input to input.txt. Set the standard output to output1.txt and output2.txt respectively. 3. Compare the two output files and output the results. Specific implementation: 1. Required header file 1: # include <stdio. h> 2: # include <stdlib. h> 3: # include <sys/file. h> 4: # include <unistd. h> 5: # include <sys/types. h> export main2.txt 1: int fdout1 = open ("stdout1.txt", O_RDWR | O_TRUNC | O_CREAT, 0); 2: int fdout2 = open ("stdout2.txt ", o_RDWR | O_TRUNC | O_CREAT, 0); // you can open two output files (3: int fdin = open ("stdin.txt", O_RDWR, 0); 4: int tempin = dup (STDIN_FILEN O); 5: int tempout = dup (STDOUT_FILENO), line = 1; // copy the standard output input Descriptor Table to prevent the system from clearing the file table 6 to which it points: char buf1 [4096], buf2 [4096]; 7: 8: dup2 (fdin, STDIN_FILENO); 9: dup2 (fdout1, STDOUT_FILENO); // redirect 10: if (system ("main1.exe") == 127) 11: write (tempout, "no exe", 7); 12: fdin = open ("stdin.txt", O_RDWR, 0); 13: dup2 (fdin, STDIN_FILENO); // Some may ask what to redirect the standard input twice. For more information, see the following. 14: dup2 (fdout2, STDOUT_FILENO); 15: if (system ("main2.exe") == 127) 16: write (tempout, "no exe", 7); 17: dup2 (tempout, STDOUT_FILENO); // restore standard output 18: lseek (fdout1, 0, SEEK_SET); 19: lseek (fdout2, 0, SEEK_SET ); // reset the File Reading location. The specific explanation is as follows. 3. process the output file. Copy code 1 for (;) 2 {3 int n; 4 5 if (n = read (fdout1, buf1, 4096)> 0 & read (fdout2, buf2, 4096)> 0) 6 {7 int I, j = 0; 8 char buffer [1024]; 9 10 for (I = 0; I <n; I ++) 11 {12 buffer [j ++] = buf1 [I]; 13 if (buf1 [I] = buf2 [I]) 14 {15 if (buf1 [I] = '\ n') 16 {17 j = 0; 18 line ++; 19} 20} 21 else22 {23 printf ("The difference in line % d between stdout1.txt and stdout2.txt \ n", line); 24 buffer [J] = '\ 0'; 25 printf ("% s \ n", buffer); 26 buffer [-- j] = buf2 [I]; 27 printf ("% s \ n", buffer); 28 break; 29 30} 31} 32 if (I! = N) 33 break; 34 35} 36 else37 {38 printf ("Can't find difference in this instance \ n"); 39 break; 40} 41} copy Code 4. The above two problems are caused by the mixed reading and writing of files with and without buffers. An error occurs when callback is referenced. Similarly, when operating the output file, the current read/write location must be restored. 5. warn that this program has a lot of bugs, but it is basically usable. In particular, I was not familiar with the programming in windows before I found it much easier to write a bat program in windows, this results in a nondescribedomainprogram written using linux commands in a windows environment. Because the program is indeed a little bad, the main purpose was to save me a terrible C language exercise that was abused by the school's evaluation website. By the way, I am familiar with the knowledge I learned, so I would like to share it with you by the way. You are welcome to point out the mistakes and be taught modestly. The current bug is: if a row exceeds B, overflow may occur. In some cases, the row number may be inaccurate. 6. Xiaogan wrote an article for the first time. I hope you will understand it.

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.