C. output the content of two files to the same screen.
Compile a program to display the content of both files on the screen at the same time, and the content of file 1 is displayed in column 1-30 on the left, and the content of file 2 is displayed in column 41-70 on the right; column 75-76 displays the total number of characters in the row of the two files, and blank characters in other columns. In addition, after 20 rows of content are output, two empty rows are output.
Let's take a look at my function implementation:
# Include
# Include
# Define size 31 char file1 [size]; char file2 [size]; int I; int readline (FILE * fp, char * file);/*** compile a program, the two files are displayed on the screen at the same time, and the content of file 1 is displayed in column 1-30 on the left, and file 2 is displayed in column 41-70 on the right; column 75-76 displays the total number of characters * in the row of the two files, and blank characters in other columns. In addition, after 20 rows of content are output, two empty rows are output. */Int main () {FILE * fp1; FILE * fp2; char fname1 [20]; // The Name Of FILE 1 char fname2 [20]; // file 2 Name // count the number of lines in the file int linecount = 0; // count the number of characters in each line int countSize = 0; // judge whether to cycle int isRun = 1; // the number of characters int count1, count2, and printf (Please enter the name of file1:); scanf (% s, fname1 ); printf (Please enter the name of file2:); scanf (% s, fname2); fp1 = fopen (fname1, r); fp2 = fopen (fname2, r ); // loop output file content while (is Run) {count1 = readline (fp1, file1); count2 = readline (fp2, file2); if (count1 = 0 & count2 = 0) break; if (count1 = 0) {printFirstSpace (0);} else {printf (% s, file1); printFirstSpace (count1);} if (count2 = 0) {printSecondSpace (0);} else {printf (% s, file2); printSecondSpace (count2);} countSize = count1 + count2; printf (% d, count1 + count2 ); linecount ++; // output 20 rows each and print 2 empty rows if (linecount = 20) {printf (); p Rintf (); linecount = 0 ;}} fclose (fp1); fclose (fp2); return 0 ;}/ *** is used to read a single string, and return the number of characters read * @ param * fp: file pointer * FILE: Save the read string */int readline (file * fp, char * file) {I = 0; int c; while (c = fgetc (fp ))! = '') {// If the end of the file is read, 0 if (feof (fp) break is returned; file [I] = c; I ++; if (I = 30) break;} file [I] = ''; return I;}/*** this method is used to output the first blank part * @ param * co1: number of characters read from the first file */void printFirstSpace (int co1) {int spaces = 30-co1 + 10; printSpaces (spaces );} /*** output the second blank part * @ param * co2: number of characters read from the second file */void printSecondSpace (int co2) {int spaces = 30-co2 + 5; printSpaces (spaces);}/*** this method is used to output blank space * @ param * spaced: number of blank spaces */void printSpaces (int spaces) {for (I = 0; I <spaces; I ++) printf ();}
The following is the running result of the program:
This program is still very interesting. Here I think of implementing a simple text Comparison Program. In the next blog, we will implement a text Comparison Program.