How to delete the last character in a file while writing

Hi while I'm using loop to write data to a file, is it possible to delete the last data that writes to the file? For example, I have the following code, where I add '+' to some information every loop that satisfy a certain condition, but in the last loop where the condition is satisfied, I do not want to add '+', so I want to delete it after writes.
for i=1:size_con
if(some condition)
fprintf(file,'some information+');
end
%I want to delete the last '+' written here
end
Note that I don't know when is the last loop that satisfies if(some condition) so I cannot solve it by splitting it up. Thank you very much!

1 Comment

More info needed. What is iterated? Please give an example of the condition (codewise)

Sign in to comment.

 Accepted Answer

I don't think that you can delete or undo a write to file after it has been written. So why not just write the '+' at the beginning of the line to account for the previous line? Presumably you will only want to do this if a line or text has already been written to file. That way, you will only write the '+' when you need to. For example,
hasTextBeenWritten = 0;
for i=1:size_con
if(some condition)
if hasTextBeenWritten
fprintf(file,'+');
end
fprintf(file,'some information');
hasTextBeenWritten = 1;
end
end
The above uses the "boolean" hasTextBeenWritten to include a '+' only if the file has had some text written to it.

More Answers (1)

In MATLAB, it is not possible to delete something that has been written to a file, not without calling a MEX routine that does the work for you. I do not know why this important I/O operation is not provided with MATLAB; any historical reason for not providing it was decades ago and weak even at the time. The underlying I/O operation is, by the way, not a deletion as such but rather that all operating systems that MATLAB is implemented on allow a file to be truncated at a given position, which discards anything after that point.
In some cases, it is possible to re-position the file writing location so that whatever is written after that will write over what has already been written. This is not the same as a deletion because if nothing else is written or a different re-position is done then the data would still be there.
The re-position call is fseek(). And to do an fseek() most easily the file needs to have been opened in binary mode (the default in MATLAB), in which case you can ask to re-position by one byte backwards. If the file is open in text mode (e.g.,
fopen('YourFile.txt', 'wt')
then the underlying I/O operations do not allow you to re-position by byte: you can only re-position to a location you obtained by using fseek(). (This is a necessary restriction to get around difficulties with what it means to move backwards by 1 right after you have written a newline, \n since in text mode that might have been automatically translated to \r\n on MS Windows systems.)
My recommendation would be similar to Geoff's: postpone writing the '+' until you know you need it.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!