Page 1 of 1

Writing between the text in a text file

Posted: Tue Dec 16, 2014 7:43 pm
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!

Re: Writing between the text in a text file

Posted: Tue Dec 16, 2014 9:13 pm
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.

Re: Writing between the text in a text file

Posted: Wed Dec 17, 2014 11:23 am
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.

Re: Writing between the text in a text file

Posted: Wed Dec 17, 2014 9:12 pm
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?

Re: Writing between the text in a text file

Posted: Fri Dec 19, 2014 3:22 pm
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 238 times
Let me know how you get on.

Re: Writing between the text in a text file

Posted: Fri Dec 19, 2014 4:47 pm
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?

Re: Writing between the text in a text file

Posted: Fri Dec 19, 2014 5:41 pm
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.

Re: Writing between the text in a text file

Posted: Fri Dec 19, 2014 11:28 pm
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!

Re: Writing between the text in a text file

Posted: Mon Dec 22, 2014 12:41 pm
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.

Re: Writing between the text in a text file

Posted: Wed Dec 24, 2014 8:57 pm
by ahmedkhalid
How long will it take to make a new Fat component?

Re: Writing between the text in a text file

Posted: Mon Dec 29, 2014 10:32 pm
by ahmedkhalid
@Benj

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