To replace a character AT A PARTICULAR POSITION in a Text FIle.

12 views (last 30 days)
I have a Text File containing many characters, I wish to modify one of those characters at a particular position
Suppose, I have a text file "1ABC.txt"
Contents of 1ABC.txt are as follows:
ADHKIGFJOFNDFBDKI
Now
from = {D,K,F};
to = {E,I,A};
position = {2,4,10};
For position in variable "position",
I have to check if the character at that position in 1ABC.txt is the same as in variable "from".
If that character is same , then I have to replace it with the character in variable "to".
Then I have to store this new line (after replacing original character in 1ABC.txt) in a different file "new.txt".
Then it will check for other positions in position variable and store the new line after replacement as a next line in the file "new.txt".
E.g. : Program will check if at the second position there is 'D' in 1ABC.txt.
Since, there is 'D' at second position of 1ABC.txt, so it will replace it with 'E'.
Save the text as new.txt after replacement and subsequent replacements as new line in "new.txt" so on loop will run .....
  2 Comments
Walter Roberson
Walter Roberson on 15 Apr 2013
For the second change, should the line output have only the second change, or should it have the effects of both the first and the second change?
If there is not a match at a position, then the line is still to be output, right?
Cedric
Cedric on 15 Apr 2013
Why doing this using files? It would be much more efficient to follow the iterative process in memory, and export to file the whole history if necessary.

Sign in to comment.

Accepted Answer

Cedric
Cedric on 15 Apr 2013
Edited: Cedric on 15 Apr 2013
I see that you accepted 0% of the answers that were given to your 4 previous questions; please take a moment for reviewing these questions/answers and accept relevant answers.
As mentioned in my comment below your question, working with memory would be much more efficient if you follow an iterative process. However, here is an example illustrating one way to achieve what you want using regular expressions:
% - Read source file.
buffer = fileread('1ABC.txt') ;
% - Extract {string, from, to, position} from content => struct.
pattern = '(?<string>^\S*).*?{(?<from>.*?)}.*?{(?<to>.*?)}.*?{(?<position>.*?)}' ;
content = regexp(buffer, pattern, 'names') ;
content.position = str2num(content.position) ;
% - Replace characters when relevant.
for k = 1 : 3
if buffer(content.position(k)) == content.from(1+2*(k-1))
buffer(content.position(k)) = content.to(1+2*(k-1)) ;
end
end
% - Export to destination file.
fid = fopen('new.txt', 'w') ;
fwrite(fid, buffer) ;
fclose(fid) ;
Note that it would be possible to make the whole pattern matching and replacement as a single call to REGEXPREP I guess, but the pattern would be significantly more complicated.

More Answers (2)

Yao Li
Yao Li on 15 Apr 2013
  2 Comments
Walter Roberson
Walter Roberson on 15 Apr 2013
Unfortunately it is not possible to seek by line number. It is possible to find some other way to determine which character position in the file is affected and then fseek() to that character position; this can be especially useful if the same character will need to be modified over and over. When determining which character position is to be modified, take into account the line terminators.
Yao Li
Yao Li on 15 Apr 2013
Since he has already obtained the position of the character which needs to be replaced (the 5th), I think the function fseek is feasible. However, if he has to get the position first, I think a while loop and the fuction fgetl() may be helpful.

Sign in to comment.


Walter Roberson
Walter Roberson on 15 Apr 2013
When you fopen(), use 'a+' as the access type.
fseek() to the beginning of file if you are going to be reading from the beginning as your method of positioning; if you already know the exact offset you are positioning to you can fseek() to there. If you arrived at your desired location by reading (rather than fseek() directly there) then you must fseek() to the position you are already in (0 bytes offset relative to current position) before you can write to the file. To emphasize, you need to fseek() if you are switching between reading and writing or between writing and reading.
Once you are at the correct location, you can fwrite() the replacement character in place. (fprintf() could also be used but using it tempts the programmer to think in terms of the output content rather than in terms of replacing an exact number of bytes.)
You can fclose() immediately after the fwrite(). Or you can fseek() to a different position for more changes.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!