Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. write at a particular position in a text document

write at a particular position in a text document

Scheduled Pinned Locked Moved C#
help
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    firefeet
    wrote on last edited by
    #1

    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

    M K 2 Replies Last reply
    0
    • F firefeet

      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

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • F firefeet

        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

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #3

        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.

        F 1 Reply Last reply
        0
        • K Keith Barrow

          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.

          F Offline
          F Offline
          firefeet
          wrote on last edited by
          #4

          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.

          K L 2 Replies Last reply
          0
          • F firefeet

            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.

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            That's even easier. Open the StreamWriter and loop around y times calling WriteLine("") each time. Next loop around x times calling Write(' ') assuming you want to use space as your padding character. Call Write 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.

            F 1 Reply Last reply
            0
            • K Keith Barrow

              That's even easier. Open the StreamWriter and loop around y times calling WriteLine("") each time. Next loop around x times calling Write(' ') assuming you want to use space as your padding character. Call Write 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.

              F Offline
              F Offline
              firefeet
              wrote on last edited by
              #6

              thank u i will try it

              1 Reply Last reply
              0
              • F firefeet

                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.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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.


                F 1 Reply Last reply
                0
                • L Luc Pattyn

                  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.


                  F Offline
                  F Offline
                  firefeet
                  wrote on last edited by
                  #8

                  luc i am sorry i am not able to understand u... can u pls explain me more

                  K 1 Reply Last reply
                  0
                  • F firefeet

                    luc i am sorry i am not able to understand u... can u pls explain me more

                    K Offline
                    K Offline
                    Keith Barrow
                    wrote on last edited by
                    #9

                    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!)

                    F 1 Reply Last reply
                    0
                    • K Keith Barrow

                      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!)

                      F Offline
                      F Offline
                      firefeet
                      wrote on last edited by
                      #10

                      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

                      T 1 Reply Last reply
                      0
                      • F firefeet

                        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

                        T Offline
                        T Offline
                        Tr v
                        wrote on last edited by
                        #11

                        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

                        F 1 Reply Last reply
                        0
                        • T Tr v

                          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

                          F Offline
                          F Offline
                          firefeet
                          wrote on last edited by
                          #12

                          ya i already looked it up. but it doesnt write to file right? or does it?

                          T 1 Reply Last reply
                          0
                          • F firefeet

                            ya i already looked it up. but it doesnt write to file right? or does it?

                            T Offline
                            T Offline
                            Tr v
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups