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. Writing to a file at the specified position

Writing to a file at the specified position

Scheduled Pinned Locked Moved C#
databasehelp
4 Posts 2 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.
  • J Offline
    J Offline
    jacobjordan
    wrote on last edited by
    #1

    I am trying to make something that will write data to a file at the specified position using the IO.StreamWriter class. Here is what i have

        public static void EncodeData(string Data, string Path, long Index)
        {
            StreamWriter stream = new StreamWriter(Path, true);
            stream.BaseStream.Position = Index;
            stream.Write(Data);
            stream.Close();
        }
    

    Problem is, that throws an exception that says "Unable seek backward to overwrite data that previously existed in a file opened in Append mode.". When i open the file so it's not in append mode, it will write at the specified position, but it will override the entire file. Just to be clear, i want this to insert the data, without overriding anything.

    void Play() { try { throw Ball(); } catch (Glove) { } finally { Play(); } } "Failure is only the opportunity to begin again, this time more wisely." "Don't ask for a light load, but rather ask for a strong back."

    W 1 Reply Last reply
    0
    • J jacobjordan

      I am trying to make something that will write data to a file at the specified position using the IO.StreamWriter class. Here is what i have

          public static void EncodeData(string Data, string Path, long Index)
          {
              StreamWriter stream = new StreamWriter(Path, true);
              stream.BaseStream.Position = Index;
              stream.Write(Data);
              stream.Close();
          }
      

      Problem is, that throws an exception that says "Unable seek backward to overwrite data that previously existed in a file opened in Append mode.". When i open the file so it's not in append mode, it will write at the specified position, but it will override the entire file. Just to be clear, i want this to insert the data, without overriding anything.

      void Play() { try { throw Ball(); } catch (Glove) { } finally { Play(); } } "Failure is only the opportunity to begin again, this time more wisely." "Don't ask for a light load, but rather ask for a strong back."

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Sounds like the value in index is not in the end of the file. However, if you have opened the file in append mode, why do you want to change position? I'm under the impression that the position is already at the end of the file.

      J 1 Reply Last reply
      0
      • W Wendelius

        Sounds like the value in index is not in the end of the file. However, if you have opened the file in append mode, why do you want to change position? I'm under the impression that the position is already at the end of the file.

        J Offline
        J Offline
        jacobjordan
        wrote on last edited by
        #3

        I don't want to append to the end of the file. I want to insert text at X position inside the file.

        void Play() { try { throw Ball(); } catch (Glove) { } finally { Play(); } } "Failure is only the opportunity to begin again, this time more wisely." "Don't ask for a light load, but rather ask for a strong back."

        W 1 Reply Last reply
        0
        • J jacobjordan

          I don't want to append to the end of the file. I want to insert text at X position inside the file.

          void Play() { try { throw Ball(); } catch (Glove) { } finally { Play(); } } "Failure is only the opportunity to begin again, this time more wisely." "Don't ask for a light load, but rather ask for a strong back."

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          In that case I believe you have to do it in parts: - create a stream for new file / memorystream - read first portion of the original file and write it to output stream - append needed data to output stream - read the second portion of the original file and write it to output stream - close the original file and write over it based on previous output stream. Of course this can be varied (read only the second portion into memory and write over it in file with new data + what's in memory etc).

          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