9 Replies - 466 Views - Last Post: 16 November 2012 - 11:22 AM Rate Topic: -----

#1 sanrooney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

//Name Age Address Tel
//============================================================
Adam,30,Malaysia,0111111111
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);



Any suggestions........? Thanks advance..

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  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 sanrooney  Icon User is offline

  • New D.I.C Head

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

Re: Insert Text inside text file at specific row

Posted 15 November 2012 - 04:30 AM

View PostJackOfAllTrades, 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

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • 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.
Was This Post Helpful? 2
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • 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.
Was This Post Helpful? 4
  • +
  • -

#7 Mideoan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 77
  • 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.
Was This Post Helpful? 1
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 Mideoan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 31-May 11

Re: Insert Text inside text file at specific row

Posted 16 November 2012 - 09:56 AM

View PostCurtis 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. :)
Was This Post Helpful? 1
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1