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