Writing data to a certain line in a file - page 2

 
VirtualReal:

The FileSeek() function only seeks on a given line (horizontal). This is not what I'm looking for, so this function won't help.

Example, you have a file like this:

Line 1 = A
Line 2 = B
Line3 = C

How can I change only the content of line 2 without the need to write lines 1 & 3 again?

I hope this example made my problem more clear.


Jim

as WHRoeder: wrote

  1. Read lines into array (line 1 array[0], line 2 array[1], line 3 array[2] )
  2. modify array (find line 2 (line 2 array[1]) & changh whatever u want)
  3. Overwrite file with array.
end of story
 
Same problem here. It's so stupid, why can't I just write inside a file? Why does it DELETES everything in the file when I'm writing into it? 
Actually came here to say that you all are <deleted>, except OP.
 
virtualreal #:


With all respect, but I'm not sure if my question is clear to you.
In case you mean the File functions, yes, I read all of them and tried Google as well.

The problem seems that it's only possible to write line after line. I'm looking for a method to change the content of a certain line, without overwriting the rest.

Jim

So, If I got your question right, I've just found out that you need to put both flags FILE_READ and FILE_WRITE into FileOpen() - and then it will not overwrite the whole file, it will only overwrite the number of bits you are writing at the place where you are writing.
Like, you tell it to write from a certain place - but how can it find it if it didn't see it??? If you didn't tell it to read it?
Not sure about seeking for lines though...

 
Mikhail Sobolev #: Same problem here. It's so stupid, why can't I just write inside a file? Why does it DELETES everything in the file when I'm writing into it? Actually came here to say that you all are jerks, except OP.

By saying it's "stupid" and calling people " <deleted> you are only showing your own ignorance and arrogance.

You can't write directly to a line, because lines of text don't have a fixed length, and writing to a short line with a longer text will just end up writing on top of the beginning of the next line, and writing a short line onto a previously long line would leave a gap and everything out of sync.

Instead, insert some humility into your judgement and learn how a file actually works and why.

 
Mikhail Sobolev #:
Same problem here. It's so stupid, why can't I just write inside a file? Why does it DELETES everything in the file when I'm writing into it? 
Actually came here to say that you all are <deleted>, except OP.

Please show respect for other forum members.

There have only been helpful suggestions in this thread.

Writing to a file does not delete everything in it, but it will overwrite part or all of the file if the coder does not move the file pointer to the correct part of the file.

 
Fernando Carreiro #:

By saying it's "stupid" and calling people " <deleted> you are only showing your own ignorance and arrogance.

You can't write directly to a line, because lines of text don't have a fixed length, and writing to a short line with a longer text will just end up writing on top of the beginning of the next line, and writing a short line onto a previously long line would leave a gap and everything out of sync.

Instead, insert some humility into your judgement and learn how a file actually works and why.

1. I'm also showing some people that they are deleted.

2. But OP had written in OP-post:
>Is there a way to write to / overwrite only specific lines in a file without overwriting all other lines
>overwriting all other lines (!)

In second comment they wrote:
> The problem seems that it's only possible to write line after line (!). I'm looking for a method to change the content of a certain line, without overwriting the rest (!).

They also wrote in the forth comment:
>When I create a text file with something like "don't overwrite this" on the first line (!), the following code overwrites this (!) and won't write to a new line.

Also they do the mistake in their code which leads to the described result:

FileOpen(SettingsFile, FILE_CSV|FILE_WRITE);

(It should be like this:)

FileOpen(SettingsFile, FILE_CSV|FILE_READ|FILE_WRITE);

In comment eight they wrote:
> How can I change  only the content of line 2  without the need to write lines 1(!) & 3 again?
> without the need to write lines 1 & 3 again (!)

etc.

So it's not about lines at all or about "writing on top of the beginning of the next line", it's about possibility to write inside a file without deleting all (!) of it's content (which happens if you do it like OP did it). 

How to write inside a file without "writing on top of the beginning of the next line" is yet to be found.

3. I did actually learned something about how file works and why, I wrote it in my next comment (№13) and I believe it was the only helpful comment in the thread. Alas, this happened eleven years later, OP probably died already without any help...

 
Keith Watford #:

Please show respect for other forum members.

There have only been helpful suggestions in this thread.

Writing to a file does not delete everything in it, but it will overwrite part or all of the file if the coder does not move the file pointer to the correct part of the file.

1. Eye for an eye. I show respect to worthy people, or even unworthy, but at least who are not unmotivatedly aggressive and deliberately rude.
What will it give to me? OP did show respect - and where did it lead them?

2. No, there were not only helpful suggestions in this thread.

Comment №2. Advice to read the manual is not helpful in any sense, it's only rude and offensive.
It does shown in the manual, that FileSeek() is used after FileOpen() with the flag FILE_READ, but it is not obvious at all and none on the "helpers" pointed at this, they did not know the manual either.

OP asked for solution "without overwriting all other lines or time consuming loops".
They asked: "I'm looking for a method to change the content of a certain line, without overwriting the rest".
And what did they get?

  1. Read lines into array
  2. modify array
  3. Overwrite file with array.

Brilliant. Is it not the opposite of "without overwriting all other lines or time consuming loops"? I think it is. Well, maybe I'm wrong. Sorry then.

Others also didn't even understand the problem, but so arrogant, so sarcastic!

3. If you open a file only with the flag FILE_WRITE and without the flag FILE_READ (as OP did it and no one noticed), then it WILL delete everything in it.

 
Mikhail Sobolev #: 3. If you open a file only with the flag FILE_WRITE and without the flag FILE_READ (as OP did it and no one noticed), then it WILL delete everything in it.

Because using both is wrong.

We already discussed that you can't write in the middle with variable length text. You can't write the beginning of the file, with variable length text either (overwrite file with array).

You must truncate the file and write to the new end.

 
William Roeder #:

Because using both is wrong.

We already discussed that you can't write in the middle with variable length text. You can't write the beginning of the file, with variable length text either (overwrite file with array).

You must truncate the file and write to the new end.

I admit, I know almost nothing about coding. But let's check something out. I took 11303152-bytes file with 262864 lines (tick history for 1 day). My code with FILE_READ|FILE_WRITE takes from 1 to 7 thousand of microseconds to change one line in that file. When only creating the array with all these lines takes from 80 to 100 thousand of microseconds. Should I continue? 
If the lines length is variable you still can rewrite only a part of the file. And that is only in case that the new line is > than the one that you need to replace (or at least I don't know yet how to add bigger lines in the middle of the file).

What does it even mean "wrong" if it works? I'm not even saying "if it is useful".

 
Mikhail Sobolev #: What does it even mean "wrong" if it works? I'm not even saying "if it is useful".

You can't modify one line with variable length text. You are creating a second small line if the replacement text is smaller. Or you are overwriting part of the next line.

It is wrong because it will not work.

Reason: