Writing between the text in a text file

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
ahmedkhalid
Posts: 28
Joined: Mon May 26, 2014 8:51 pm
Has thanked: 4 times
Contact:

Writing between the text in a text file

Post by ahmedkhalid »

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!

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Writing between the text in a text file

Post by kersing »

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

User avatar
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

Post by Benj »

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.

ahmedkhalid
Posts: 28
Joined: Mon May 26, 2014 8:51 pm
Has thanked: 4 times
Contact:

Re: Writing between the text in a text file

Post by ahmedkhalid »

kersing wrote:The only way to do this is to read the file contents, modify it and write it back.
ok reading the content is fine but how to modify it and write it back? can u show me a simple simulation please?

User avatar
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

Post by Benj »

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.
FAT_Insert.fcfx
(16.01 KiB) Downloaded 204 times
Let me know how you get on.

ahmedkhalid
Posts: 28
Joined: Mon May 26, 2014 8:51 pm
Has thanked: 4 times
Contact:

Re: Writing between the text in a text file

Post by ahmedkhalid »

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?

User avatar
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

Post by Benj »

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.

ahmedkhalid
Posts: 28
Joined: Mon May 26, 2014 8:51 pm
Has thanked: 4 times
Contact:

Re: Writing between the text in a text file

Post by ahmedkhalid »

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!

User avatar
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

Post by Benj »

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.

ahmedkhalid
Posts: 28
Joined: Mon May 26, 2014 8:51 pm
Has thanked: 4 times
Contact:

Re: Writing between the text in a text file

Post by ahmedkhalid »

How long will it take to make a new Fat component?

ahmedkhalid
Posts: 28
Joined: Mon May 26, 2014 8:51 pm
Has thanked: 4 times
Contact:

Re: Writing between the text in a text file

Post by ahmedkhalid »

@Benj

Did you try your code of FAT insert? Is it working?

Post Reply