Insert Text inside text file at specific row

Page 1 of 1

9 Replies - 622 Views - Last Post: 16 November 2012 - 11:22 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300311&amp;s=88df06eb9e39b6e176c265f15b188090&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 sanrooney

Reputation: 0
• Posts: 35
• Joined: 25-April 11

Insert Text inside text file at specific row

Posted 15 November 2012 - 04:12 AM

Hi,

can anybody help me on this. I have a text file with header and footer
I want to insert a row of data before the footer.

This is the sample of text file :

//Student Information

//============================================================
Shah,24,Malaysia,0122334455

//End Of File

I have try this code but it adds the line after the "//End Of File"

```File.AppendAllText(appPath+@"\test\new.txt",label1.Text + Environment.NewLine);

```

This post has been edited by sanrooney: 15 November 2012 - 04:13 AM

Is This A Good Question/Topic? 0

Replies To: Insert Text inside text file at specific row

#2 coder3788

Reputation: 38
• Posts: 62
• Joined: 06-November 12

Re: Insert Text inside text file at specific row

Posted 15 November 2012 - 04:19 AM

One idea could be to insert some kind of special string at the start of the footer, that you can be sure won't be part of the text, like \$\$\$footer\$\$\$ or something.
Then, you can search that string ,go up one row and insert what you need.

• Saucy!

Reputation: 6208
• Posts: 23,953
• Joined: 23-August 08

Re: Insert Text inside text file at specific row

Posted 15 November 2012 - 04:21 AM

I'm quite sure this assignment is all about getting you to think about the right way to do this, so I won't give the answer, but the key is this: You can't just insert directly into a text file at a specific row; it's just not possible.

#4 sanrooney

Reputation: 0
• Posts: 35
• Joined: 25-April 11

Re: Insert Text inside text file at specific row

Posted 15 November 2012 - 04:30 AM

JackOfAllTrades, on 15 November 2012 - 04:21 AM, said:

I'm quite sure this assignment is all about getting you to think about the right way to do this, so I won't give the answer, but the key is this: You can't just insert directly into a text file at a specific row; it's just not possible.

hahaha ya your right but I have spend 4 days to find a solution for this. I don't find the
right way actualy that's why i'm here now.

This post has been edited by sanrooney: 15 November 2012 - 04:31 AM

#5 Skydiver

• Code herder

Reputation: 4265
• Posts: 13,652
• Joined: 05-May 12

Re: Insert Text inside text file at specific row

Posted 15 November 2012 - 06:15 AM

Think of using temporary storage: either using memory if you know the file will be small, or using temporary file if the file can grow very big.

#6 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4834
• Posts: 8,613
• Joined: 08-June 10

Re: Insert Text inside text file at specific row

Posted 15 November 2012 - 07:23 AM

Ask yourself this: if you were using a typewriter, and someone told you to add some text to the middle of page that didn't have room for it, what would you do? The answer is the same for file IO.

#7 Mideoan

Reputation: 4
• Posts: 78
• Joined: 31-May 11

Re: Insert Text inside text file at specific row

Posted 15 November 2012 - 12:23 PM

You could always delete the final row, append your new line, and then add the //End of file text line back.

#8 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4834
• Posts: 8,613
• Joined: 08-June 10

Re: Insert Text inside text file at specific row

Posted 15 November 2012 - 01:53 PM

So, with three of us here, trying to lead him to understanding on his own, you thought it would be a good idea to just dump the answer? We don't do people's work for them.

#9 Mideoan

Reputation: 4
• Posts: 78
• Joined: 31-May 11

Re: Insert Text inside text file at specific row

Posted 16 November 2012 - 09:56 AM

Curtis Rutland, on 15 November 2012 - 01:53 PM, said:

So, with three of us here, trying to lead him to understanding on his own, you thought it would be a good idea to just dump the answer? We don't do people's work for them.

Sorry if mine posting offended you. My input was intended to put him/her on the correct path. Understanding what needs to be done and writing the code are two different avenues in my opinion. Just trying to help contribute since I've done my fair share of taking when I was learning.

My apologies.

• Saucy!

Reputation: 6208
• Posts: 23,953
• Joined: 23-August 08

Re: Insert Text inside text file at specific row

Posted 16 November 2012 - 11:22 AM

Sigh.

Quote

Understanding what needs to be done and writing the code are two different avenues in my opinion.

You are correct, but both are equally important. And as I stated above, it's QUITE OBVIOUS the point was to get the OP thinking about understanding what needs to be done. When I was typing my initial reply, I started down the path of providing the different means by which to do this, when it occurred to me that was really the whole point of the exercise, so I removed it. I was hoping to get something further from the OP regarding his thought process, but it's apparent they (like so many others who come here) had checked out after "four days" of looking at this. Skydiver and Curtis Rutland went on and provided a little more prodding in the right direction to get him thinking, which was the absolute right thing to do.

Thank you nonetheless for trying to make a contribution, that's always appreciated.