
rovf
Veteran
Jun 28, 2011, 11:32 AM
Post #7 of 15
(730 views)
|
|
Re: [harshmane] to interchange line no of file & print.
[In reply to]
|
Can't Post
|
|
(1) I guess you mean "file handle" and not "file handler". Correct? These are very different things, so please be careful with your wording. (2) You arfe using 2 file handles, not one. However, since you are dealing with two files, you WILL need 2 file handles. If your "Sir" asks you to use only one, you have two choices: Either kick him at the place just below his spine (this would be the best), or first read the whole file into memory, close it, open the same file handle for writing, write the change file, and after this kick your Sir into the buttocks at least twice. (3) I don't think it is a good idea to use "goto". (4) You don't need to count the lines. It is trivial to implement tis for a file ahving any number of lines. Otherwise, your usage of "goto" is technically correct, just that nobody being more sane than the zombies depicted in "The Night Of The Living Dead" would program it that way. A "while" loop would be absolutely fine here. (5) (Now this is the most important observation for your problem): In every iteration of your loop, you are reading 2 lines, but you are writing at most one. This means that h3.txt will contain at most 2 lines (one for $i equals 0, one for $i equals 1). You will have to write 2 lines in each iteration, lest your output file won't have the same number of lines than your input file.
|