To replace a character AT A PARTICULAR POSITION in a Text FIle.
조회 수: 12 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2013년 4월 15일
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
2013년 4월 15일
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.
채택된 답변
Cedric
2013년 4월 15일
편집: Cedric
2013년 4월 15일
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.
추가 답변 (2개)
Yao Li
2013년 4월 15일
Try
fseek()
댓글 수: 2
Walter Roberson
2013년 4월 15일
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
2013년 4월 15일
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.
Walter Roberson
2013년 4월 15일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!