Writing between the text in a text file
Moderator: Benj
-
- Posts: 28
- Joined: Mon May 26, 2014 8:51 pm
- Has thanked: 4 times
Writing between the text in a text file
Hello everyone!
I have got a small problem here and need your help. I want to make a program which inserts a text in a text file using FAT component of flowcode. The thing is
the text file is already written with text and the text which needs to be insert have to reside between particular texts. Obviously there will be a condition to
check where the data/text resides. e.g. consider a text file having written my name "Ahmed Khalid". Now the thing is I want to insert the name "Benj"
between my first and last name. How this can be done? Hope you guys get my question!
Thanks in advance!
I have got a small problem here and need your help. I want to make a program which inserts a text in a text file using FAT component of flowcode. The thing is
the text file is already written with text and the text which needs to be insert have to reside between particular texts. Obviously there will be a condition to
check where the data/text resides. e.g. consider a text file having written my name "Ahmed Khalid". Now the thing is I want to insert the name "Benj"
between my first and last name. How this can be done? Hope you guys get my question!
Thanks in advance!
-
- Valued Contributor
- Posts: 2045
- Joined: Wed Aug 27, 2008 10:31 pm
- Location: Netherlands
- Has thanked: 553 times
- Been thanked: 1081 times
Re: Writing between the text in a text file
FAT does not allow inserting data somewhere in a file. The only way to do this is to read the file contents, modify it and write it back. If the contents may be too large for the controllers memory you will have to read from one file and write the modified data to another file.
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Writing between the text in a text file
Hello,
You could do something like this using the circular buffer component, probably with at least 512 bytes plus the size of the data you want to add.
You would have to read the bytes in the file until you come across your match for the place where you want to insert your data.
You would then have to read the rest of the bytes from the 512 byte sector buffer and load them into the circular buffer component. Next overwrite your appended data into the buffer at the correct position and then start to pull bytes back out of the circular buffer and writing back to the buffer. At the end of the 512 bytes move to the next sector and again pull all the bytes back out into the circular buffer. Then repeat the process of pulling bytes back out the circular buffer and overwriting the 512 FAT buffer.
Rinse and repeat until you reach the end of the file and there you have inserted your data.
Not the easiest thing to do but it should be possible.
You could do something like this using the circular buffer component, probably with at least 512 bytes plus the size of the data you want to add.
You would have to read the bytes in the file until you come across your match for the place where you want to insert your data.
You would then have to read the rest of the bytes from the 512 byte sector buffer and load them into the circular buffer component. Next overwrite your appended data into the buffer at the correct position and then start to pull bytes back out of the circular buffer and writing back to the buffer. At the end of the 512 bytes move to the next sector and again pull all the bytes back out into the circular buffer. Then repeat the process of pulling bytes back out the circular buffer and overwriting the 512 FAT buffer.
Rinse and repeat until you reach the end of the file and there you have inserted your data.
Not the easiest thing to do but it should be possible.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 28
- Joined: Mon May 26, 2014 8:51 pm
- Has thanked: 4 times
Re: Writing between the text in a text file
ok reading the content is fine but how to modify it and write it back? can u show me a simple simulation please?kersing wrote:The only way to do this is to read the file contents, modify it and write it back.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Writing between the text in a text file
Hello,
Right I have not tested this but it should be something like correct.
I basically load the circular buffer with the data I want to insert.
I then move to the point in the file where I want to insert data and pull off any data after that from the sector into the circular buffer before re-filling the sector buffer with the modified file contents.
Then repeat for the other sectors in the file, read the entire sector into the circular buffer and then write the contents of the circular buffer back to the sector buffer and write back to the card. If the data then goes over the last current sector in the file then we create a new sector to allow the remaining data to be added.
Let me know how you get on.
Right I have not tested this but it should be something like correct.
I basically load the circular buffer with the data I want to insert.
I then move to the point in the file where I want to insert data and pull off any data after that from the sector into the circular buffer before re-filling the sector buffer with the modified file contents.
Then repeat for the other sectors in the file, read the entire sector into the circular buffer and then write the contents of the circular buffer back to the sector buffer and write back to the card. If the data then goes over the last current sector in the file then we create a new sector to allow the remaining data to be added.
Let me know how you get on.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 28
- Joined: Mon May 26, 2014 8:51 pm
- Has thanked: 4 times
Re: Writing between the text in a text file
I ran your simulation. The code seems to work but what happens is the file occupies some space for text but the text does not appear.
Even the already written text got disappear. I did not run it on hardware though. Did you test the simulation now?
Even the already written text got disappear. I did not run it on hardware though. Did you test the simulation now?
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Writing between the text in a text file
Hello,
I would test on the embedded hardware, the FAT simulation can be a bit temperamental when it comes to doing things like this as Windows starts to fight back.
I would test on the embedded hardware, the FAT simulation can be a bit temperamental when it comes to doing things like this as Windows starts to fight back.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 28
- Joined: Mon May 26, 2014 8:51 pm
- Has thanked: 4 times
Re: Writing between the text in a text file
Moreover, the code seems to be a bit tricky.
Ben can u make a function for the FAT component which can insert a text at anywhere in the text file? It will be a great help!
Ben can u make a function for the FAT component which can insert a text at anywhere in the text file? It will be a great help!
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Writing between the text in a text file
Hello,
I can't really add a function without massively increasing the RAM overhead required by the FAT component. e.g. adding enough to allow the circular buffer to function even if it's not being used.
This would likely break a lot of current existing programs which is a big problem.
I'll think on it, could be we make a 2nd FAT component which has the functionality built in and won't break existing programs.
I can't really add a function without massively increasing the RAM overhead required by the FAT component. e.g. adding enough to allow the circular buffer to function even if it's not being used.
This would likely break a lot of current existing programs which is a big problem.
I'll think on it, could be we make a 2nd FAT component which has the functionality built in and won't break existing programs.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 28
- Joined: Mon May 26, 2014 8:51 pm
- Has thanked: 4 times
-
- Posts: 28
- Joined: Mon May 26, 2014 8:51 pm
- Has thanked: 4 times
Re: Writing between the text in a text file
@Benj
Did you try your code of FAT insert? Is it working?
Did you try your code of FAT insert? Is it working?