write at a particular position in a text document
-
hi everyone. i am looking for some help with my project. i have x and y position and a string. i want to write that string in a text document at that x and y position. can anyone pls help me
Does x and y represent a character index and a row index? If you want to edit a text document then you need to read it, edit it, and write it to another file (temp) and then rename that file (with a File.Move) - unless you want to read all the file into memory then write it straight back to the original file, depends on file size really but I would avoid either way. So you could use a System.IO.StreamReader to read the file a line at a time and write each one to temp file. Then when you get to the line you want to edit, add the data at the required position and write that line ti temp file. Then write remaining lines.
Life goes very fast. Tomorrow, today is already yesterday.
-
hi everyone. i am looking for some help with my project. i have x and y position and a string. i want to write that string in a text document at that x and y position. can anyone pls help me
Here is one method: 1. Open a StreamReader 2. Open a StreamWriter to the new file 3. Do a ReadLine on the StreamReader y times, writing the result to the StreamWriter using WriteLine 4. On the Stream Reader Do a ReadBlock(x) and call Write on the StreamWriter, passing in the result. 5. Write the text to be inserted 6. Call StreamReader.ReadToEnd() and write the results to file using the Writer. There are a couple of things to look out for: a) The algorith I have given you writes to the correct position. There could be out-by-one errors (e.g. writing to a character or line before of after the one desired) as I haven't tested it. b) You'll get exceptions if y is greater than the length of the file. Similarly, if x is greater than the length of line y, an exception will be thrown Take a look at http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx[^], the "HowTos" at the bottom contain "How to Read from File" and "How to Write to File" links which will be useful.
-
Here is one method: 1. Open a StreamReader 2. Open a StreamWriter to the new file 3. Do a ReadLine on the StreamReader y times, writing the result to the StreamWriter using WriteLine 4. On the Stream Reader Do a ReadBlock(x) and call Write on the StreamWriter, passing in the result. 5. Write the text to be inserted 6. Call StreamReader.ReadToEnd() and write the results to file using the Writer. There are a couple of things to look out for: a) The algorith I have given you writes to the correct position. There could be out-by-one errors (e.g. writing to a character or line before of after the one desired) as I haven't tested it. b) You'll get exceptions if y is greater than the length of the file. Similarly, if x is greater than the length of line y, an exception will be thrown Take a look at http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx[^], the "HowTos" at the bottom contain "How to Read from File" and "How to Write to File" links which will be useful.
-
thank u..... actually i have to create a new file and write the text at a particular X and y position. how do i do that? i have the position X and Y in pixels.
That's even easier. Open the StreamWriter and loop around y times calling
WriteLine("")
each time. Next loop around x times callingWrite(' ')
assuming you want to use space as your padding character. CallWrite
again, passing in the text you want to insert. Again, you'll need to check the output file to esure the method I have given you is correct as I haven't checked it and it might all be out by one line and/or character. -
That's even easier. Open the StreamWriter and loop around y times calling
WriteLine("")
each time. Next loop around x times callingWrite(' ')
assuming you want to use space as your padding character. CallWrite
again, passing in the text you want to insert. Again, you'll need to check the output file to esure the method I have given you is correct as I haven't checked it and it might all be out by one line and/or character. -
thank u..... actually i have to create a new file and write the text at a particular X and y position. how do i do that? i have the position X and Y in pixels.
That doesn't make any sense. Text holds characters and doesn't know about fonts and sizes, so pixels don't exist in the world of text. Formatted text (such as a Word document, a Wordpad document, an HTML document) knows about formatting, but strictly speaking still does not know about exact pixel location. If it is formatted text you're after, first thing to decide is which format to use. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
That doesn't make any sense. Text holds characters and doesn't know about fonts and sizes, so pixels don't exist in the world of text. Formatted text (such as a Word document, a Wordpad document, an HTML document) knows about formatting, but strictly speaking still does not know about exact pixel location. If it is formatted text you're after, first thing to decide is which format to use. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
You'd confused your terminology. Pixels are the small dots that make up the screen display, normally associated with screen resolutions and graphins files. e.g. a screen might show 1024 x 768 pixels, a bitmap is 100 pixel x 100 pixels etc You used "Pixel" where you meant a character position (I think!)
-
You'd confused your terminology. Pixels are the small dots that make up the screen display, normally associated with screen resolutions and graphins files. e.g. a screen might show 1024 x 768 pixels, a bitmap is 100 pixel x 100 pixels etc You used "Pixel" where you meant a character position (I think!)
i have x and y in pixels only. i get that x and y from a xml. this xml is generated from a web application which contains many dynamically created 'div's. user will be able be move these div s around. on clicking save i store their pixel positions as attribute in xml. now i need to write the value associated with each div s( which are again saved as an attribute) in their corresponding positions( ie x and y position).... can anyone help me... pls
modified on Thursday, August 20, 2009 7:43 AM
-
i have x and y in pixels only. i get that x and y from a xml. this xml is generated from a web application which contains many dynamically created 'div's. user will be able be move these div s around. on clicking save i store their pixel positions as attribute in xml. now i need to write the value associated with each div s( which are again saved as an attribute) in their corresponding positions( ie x and y position).... can anyone help me... pls
modified on Thursday, August 20, 2009 7:43 AM
-
You could use the Graphics.DrawString method which will take either a System.Drawing.Point or x and y coordinates in pixels. Just look up DrawString on MSDN for documentation.
modified on Thursday, August 20, 2009 5:08 PM
-
No, it won't write to a text file. Like it was already said, text files aren't measured in pixels. If you have to save it to a file, the only way would be to save it in some image format. I suppose if you knew your printers resolution (dots per inch, etc..) you could print to a certain x,y location on the page, but as far as a text doc goes, there just aren't any measurements in pixels, so I don't believe it is possible to do exactly what you are wanting to do.