How to add a lineat the beginnig of a file?
-
:confused:I need to insert a new line at the beginning of a text file but i don´t know how to do this without overwriting the first line. This is my code: ... file.SeekToBegin(); file.WriteString(text); ... I've also tried this: ... file.SeekToBegin(); file.WriteString("\r\n"); file.WriteString(text); ... but it doesn´t work. Could anyone help me? Thanks Lucia
-
:confused:I need to insert a new line at the beginning of a text file but i don´t know how to do this without overwriting the first line. This is my code: ... file.SeekToBegin(); file.WriteString(text); ... I've also tried this: ... file.SeekToBegin(); file.WriteString("\r\n"); file.WriteString(text); ... but it doesn´t work. Could anyone help me? Thanks Lucia
-
:confused:I need to insert a new line at the beginning of a text file but i don´t know how to do this without overwriting the first line. This is my code: ... file.SeekToBegin(); file.WriteString(text); ... I've also tried this: ... file.SeekToBegin(); file.WriteString("\r\n"); file.WriteString(text); ... but it doesn´t work. Could anyone help me? Thanks Lucia
It is not possible to 'insert' new data at the begining of the file. A solution for doing that is using a temporary file. In this file, you first copy the first line you need to insert, then copy all the data from the original file. When this is done, you take this data and copy it into the original file (erasing all it's previous contents).
-
It is not possible to 'insert' new data at the begining of the file. A solution for doing that is using a temporary file. In this file, you first copy the first line you need to insert, then copy all the data from the original file. When this is done, you take this data and copy it into the original file (erasing all it's previous contents).
-
It is not possible to 'insert' new data at the begining of the file. A solution for doing that is using a temporary file. In this file, you first copy the first line you need to insert, then copy all the data from the original file. When this is done, you take this data and copy it into the original file (erasing all it's previous contents).
Thanks for the reply. I have already thought of this solution but i searched the simplest one. Ok, i'll try using a temporary file
-
Thanks for the reply. I have already thought of this solution but i searched the simplest one. Ok, i'll try using a temporary file
Be sure you have write access to the folder in which the temporary file will be created. If the file is not gigantic (e.g., 100MB or more), you could just read it into a heap-based buffer, write the new text to the file and then append the contents of the buffer. That's about as simple as it gets.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow