如何编写以下问题的代码 [英] How do I write code for the following question

查看:62
本文介绍了如何编写以下问题的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个文件。你必须将两个文件中的备用行合并到第三个.dat文件中并显示它的内容。



我尝试了什么:



我尝试了以下内容:

从文件1中读取一行...然后写入文件3然后..从下一行读取文件1 ..然后在文件3中读取文件2 ..write中的一行..然后从文件2中读取下一行..现在写入。文件1等等..

There are two files. You have to merge alternate lines from both files into a third ".dat" file and display it's contents.

What I have tried:

I have tried the following :
read a line from file 1..then write in file 3 then.. read next line from file 1..then read a line from file 2 ..write in file 3.. then read next line from file 2 .. now write from. file 1 and so on..

推荐答案

这是一个整体的想法 - 除了一点: - 你正在读A中的两行,一行来自B,一行来自A ,...

That's the whole idea - except for one bit: - you are reading two lines from A, one from B, one from A, ...
1) Open File1 for reading
2) Open File2 for reading
3) Open File3 for writing
4) Loop
4.1) If File1 not at end then
4.1.1) Read line from file 1
4.1.2) Write to File3
4.2) If File2 not at end then
4.2.1) Read line from file 2
4.2.2) Write to File3
4.3) Continue loop until File1 and File2 are at the end.
5) Close file1
6) Close File2
7) Close File3
8) Open File3 for reading
9) Loop
9.1) If File3 at end then
9.1.1) break out of loop
9.2) Read line from File3
9.3) Write to console
9.4) Continue loop forever.
10) Close File3


这篇关于如何编写以下问题的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆