Simultaneously implement the content of two files to implement the content of the two files

Source: Internet
Author: User

Simultaneously implement the content of two files to implement the content of the two files

Literally, the content of the two files is on the same horizontal screen. File 1 occupies 1-30 columns, and 20 columns in the middle are separated by spaces. File 2 occupies the remaining 30 columns.

Next I will talk about my ideas:

For example, the horizontal length of the two articles is set to 80. In File 1, enter 1-30 characters (if it is enough, replace it with a space character), and then separate it with 20 spaces, then, enter file 2 with 30 characters (if the input is sufficient, add a space) to complete the input of a line. In this case, we can simply enter a line in an External Loop and 80 characters in an internal loop. The number of rows is uncertain, because I don't know the content of the file (even if I know the computation), but the number of columns is definite, that is, 80, not much. So how does one end the input row and exit? That is, when the content of both files is input. Because it is possible that file 1 has been input, but file 2 has not been input, or it may be reversed. It is worth noting that the file Terminator EOF and line break '\ n', because it also determines when to fill the number of characters with spaces.

The following code is provided:

1/* open two files at the same time */2 # include <stdio. h> 3 # include <stdlib. h> 4 5 # define MAX 80 6 7 void Readline (FILE * fp1, FILE * fp2); 8 9 int main (void) 10 {11 FILE * f1, * f2; 12 char filename [256]; 13 14 printf ("enter the name of file 1:"); 15 scanf ("% s", filename ); 16 17 if (f1 = fopen (filename, "r") = NULL) 18 {19 perror ("cannot read"); 20 exit (1 ); 21} 22 23 printf ("enter the name of file 2:"); 24 scanf ("% s", filename); 25 26 if (f2 = fopen (filename, "r") = NULL) 27 {28 perror ("cannot read"); 29 exit (1); 30} 31 32 Readline (f1, f2 ); /* column sharding function */33 34 fclose (f1); 35 fclose (f2); 36 return 0; 37} 38 39 40 41 42 void Readline (FILE * fp1, FILE * fp2) 43 {44 int count;/* indicates the number of columns */45 char records, ch2; 46 47 records = fgetc (fp1); 48 ch2 = fgetc (fp2 ); 49 while (1) 50 {51 for (count = 1; count <= MAX; count ++)/* each column is displayed, no space is displayed */52 {53 if (count> = 1 & count <= 30) /* the range of file 1 */54 {55 if (Bytes = EOF) | (Bytes = '\ n') 56 {57 fprintf (stdout, "% c", ''); 58} 59 else60 {61 fprintf (stdout," % c ", outputs); 62 outputs = fgetc (fp1 ); 63} 64} 65 if (count> 30 & count <= 50)/* use a space to separate the content of the two files */66 {67 fprintf (stdout, "% c", ''); 68} 69 70 71 if (count> 50 & count <= MAX) /* the range of file 2 */72 {73 if (ch2 = EOF) | (ch2 = '\ n') 74 {75 fprintf (stdout, "% c", ''); 76} 77 else78 {79 fprintf (stdout," % c ", ch2); 80 ch2 = fgetc (fp2 ); 81} 82} 83 84} 85 86 if (delimiter = '\ n')/* if the Delimiter is a line break, if the next character is not obtained, the output will always contain spaces */87 leading = fgetc (fp1); 88 if (ch2 = '\ n') 89 ch2 = fgetc (fp2 ); /* the same is true for ch2. The two cannot be combined because they are independent */90 91 fprintf (stdout, "% c", '\ n '); 92 if (condition = EOF) & (ch2 = EOF)/* exit condition */93 break; 94} 95}View Code

 

Use two test files to open the file and output the file to the terminal. You can also modify the file. The result is as follows:

In fact, the reason is very simple, but you must pay attention to the line break problem. I tried to swallow a letter for each line, because the line feed gets a character and the next output gets the character again and overwrites it. The expression that obtains the next character can be executed only when the line feed is completed.

What can be improved: This program only supports ascll code. If it is a Chinese character, garbled characters will appear, because Chinese characters seem to occupy two bytes. In addition, you can add a count next to the program to calculate the total number of characters in each line except spaces.

I am a newbie. I hope to point out something wrong. Thank you.

 

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.