Click here to Skip to main content
14,695,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all,

I am checking a notepad file which contains almost 5000 lines of records.
I have to insert 10 rows of records after each 500 lines in the same file.
After doing the job I will have a file which will contain total 5100 lines of records.
Original File
--------------
00001
00002
.
.
00500 <-- New 10 rows after this record
.
.
09999
10000
--------------


I am using System.IO namespace for reading purpose.

Tools:

Framework = 2.0
Language = VB.NET
.
Please Advice.
Thanks In Advance.
Posted
Updated 4-Jan-12 2:35am
v2

There's no 'insertion' functionality for files: you have to create a new file with the appropriate content:
Read line by line from input file (you may use the StreamReader.ReadLine Method[^]) and output the read line to the ouput file, until line count reaches 500.
Then write the '10 rows' to the output file and, finally, resume reading line by line the input file (and write every line to the output file) until the end-of-(input)file.
   
Comments
Wendelius 4-Jan-12 17:51pm
   
That'll be a good way. 5. I added a bit brute-force solution...
Harshad-HBK 6-Jan-12 0:26am
   
Thanks CPallini..I got ma solution
CPallini 7-Jan-12 15:55pm
   
You are welcome.
One quite brute-force way is to read all the lines to an array or a list, add necessary items and then write the contents to the file. For example something like:
List<string> lines = System.IO.File.ReadAllLines("...").ToList<string>();
lines.Insert(500, "some string");
lines.Insert(501, "other string");
...
System.IO.File.WriteAllLines("...",lines);
   
Comments
CPallini 4-Jan-12 18:19pm
   
My 5.
Wendelius 4-Jan-12 18:41pm
   
Thanks :)
Harshad-HBK 6-Jan-12 0:27am
   
Thanks Mika, job done successfully...
Wendelius 6-Jan-12 6:10am
   
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900