How can I move to a specific position in a text file and write a text there?
example:
volatile real32_T AutoWakeUpBalncDlyT_T_Pt = 3.0F;/* Delay time to distinguish to go to sleep. */
before
(AutoWakeUpBalncDlyT_T_Pt)
I have to add
Rom_
expected :
volatile real32_T Rom_AutoWakeUpBalncDlyT_T_Pt = 3.0F;/* Delay time to distinguish to go to sleep. */

댓글 수: 1

ANAND VISHAL
ANAND VISHAL 2020년 2월 26일

AutoWakeUpBalncDlyT_T_Pt name is different in text file then? only First word volatile is constant in text file. I have to insert at third position after real32_T and real32_T may also change.

댓글을 달려면 로그인하십시오.

 채택된 답변

darova
darova 2020년 2월 26일

0 개 추천

What about strrep?
old = 'AutoWakeUpBalncDlyT_T_Pt';
new = 'Rom_AutoWakeUpBalncDlyT_T_Pt';
fid = fopen('test1.txt','r');
str1 = fscanf(fid,'%c');
fclose(fid);
str2 = strrep(str1,old,new);
fid = fopen('test2.txt','w');
fprintf(fid,'%c',str2);
fclose(fid);

댓글 수: 30

ANAND VISHAL
ANAND VISHAL 2020년 2월 26일
AutoWakeUpBalncDlyT_T_Pt name is different in text file then? only First word volatile is constant in text file. I have to insert at third position after real32_T and real32_T may also change.
darova
darova 2020년 2월 26일
I don't understand. Did you try my script?
HI @darova u are correct:but my requirement is different
Assume this is input text file:
volatile uint8_T AtomcMduleCnt_Z_Pt = 7U;/* Atomic Module Count in Pack */
volatile real32_T AutoWakeUpBalncDlyT_T_Pt = 3.0F;/* Delay time to distinguish to go to sleep. */
volatile real32_T AvgChrgOcUWrtSoc_U_Yy[29] = { 410.05F, 413.78F, 416.49F,
volatile real32_B AvgDschgOcUAt2Pc_U_Pt = 317.49F;/* Average pack nominal voltage at 2% SOC at 25 degC at 2 C-rate. */
Expected output for text:
volatile uint8_T Rom_AtomcMduleCnt_Z_Pt = 7U;/* Atomic Module Count in Pack */
volatile real32_T Rom_AutoWakeUpBalncDlyT_T_Pt = 3.0F;/* Delay time to distinguish to go to sleep. */
volatile real32_T Rom_AvgChrgOcUWrtSoc_U_Yy[29] = { 410.05F, 413.78F, 416.49F,
volatile real32_B Rom_AvgDschgOcUAt2Pc_U_Pt = 317.49F;/* Average pack nominal voltage at 2% SOC at 25 degC at 2 C-rate. */
How can I move to a specific position in a text file and write a text there?
darova
darova 2020년 2월 27일
편집: darova 2020년 2월 27일
Try this
fid = fopen('test1.txt','r');
str1 = fscanf(fid,'%c');
fclose(fid);
str2 = strsplit(str1); % convert each worf to cell
k = strcmp(str2,'volatile'); % get each position of "volatile"
k1 = find(k);
str3 = str2;
for i = 1:length(k1)
s = char( str3(k1(i)+2) ); % position after "volatile"
str3(k1(i)+2) = {['Rom_' s]}; % insert "Rom_"
str3(k1(i)) = {[sprintf('\n') char(str3(k1(i)))]}; % insert brealine/newline
end
fid = fopen('test2.txt','wt'); % open file in "write" and "text" mode
fprintf(fid,'%c',strjoin(str3)); % write new string
fclose(fid);
ANAND VISHAL
ANAND VISHAL 2020년 2월 27일
This is right but its come in cell but i need in string
Can u help me
Thanks in advanced.
darova
darova 2020년 2월 27일
i don't understand. Where is the problem
ANAND VISHAL
ANAND VISHAL 2020년 2월 27일
% convert each word to cell is the problem I need in cell to string that it all the text file come in single line this is the main problem. I need to keep the text file as original
darova
darova 2020년 2월 27일
Here is the result of script execution
I don't understand where is the problem
ANAND VISHAL
ANAND VISHAL 2020년 2월 28일
편집: ANAND VISHAL 2020년 2월 28일
@darova please look into its its urgent
I have upload both input and output file.
if u compare input and output file then u know the difference
So please check......
we have to write the script in such a ways that with the help of input txt file we get the outputfile.
darova
darova 2020년 2월 28일
I am working on it
This should work
fid = fopen('test11.txt','r');
str1 = fscanf(fid,'%c');
fclose(fid);
k = strfind(str1,'volatile'); % get each position of "volatile"
k1 = strfind(str1,' '); % get each position of "volatile"
[k2,ix] = sort([k k1]); % sort 'spaces' and 'volatile' positions
[~,ind] = sort(ix);
ii = ind(1:length(k)); % indices of 'volatile'
str2 = str1;
for i = length(ii):-1:1
kk = k2(ii(i)+2); % index of 2d space after 'volatile'
str2 = [str2(1:kk) 'Rom_' str2(kk+1:end)];
end
fid = fopen('test22.txt','wt'); % open file in "write" and "text" mode
fprintf(fid,'%c',str2); % write new string
fclose(fid);
ANAND VISHAL
ANAND VISHAL 2020년 2월 28일
Thanks you so much @darova.
darova
darova 2020년 2월 28일
@darova i need to integrate these two code:
fid = importdata('input.txt');
b=size(fid);
a = fid;
for i= 1:b
str='volatile';
str1='data';
str2='.CALIB_RAM_DATA_SECTION';
a(i) = strrep(a(i),str,'volatile const');
a(i) = strrep(a(i),str1,'rodata');
a(i) = strrep(a(i),str2,'.CALIB_ROM_DATA_SECTION');
end
and
fid = fopen('input.txt','r');
str1 = fscanf(fid,'%c');
fclose(fid);
k = strfind(str1,'volatile'); % get each position of "volatile"
k1 = strfind(str1,' '); % get each position of "volatile"
[k2,ix] = sort([k k1]); % sort 'spaces' and 'volatile' positions
[~,ind] = sort(ix);
ii = ind(1:length(k)); % indices of 'volatile'
str2 = str1;
for i = length(ii):-1:1
kk = k2(ii(i)+2); % index of 2d space after 'volatile'
str2 = [str2(1:kk) 'Rom_' str2(kk+1:end)];
end
fid = fopen('test22.txt','wt'); % open file in "write" and "text" mode
fprintf(fid,'%c',str2); % write new string
fclose(fid);
then i will able to get desired output
u can refer input and output file for understanding which are in previous comments.
darova
darova 2020년 2월 29일
See attached script
ANAND VISHAL
ANAND VISHAL 2020년 2월 29일
@darova thanks you so much
darova
darova 2020년 2월 29일
You are welcome
ANAND VISHAL
ANAND VISHAL 2020년 3월 2일
what is the use of sort function in the code?
First you find positions/indices of
k = strfind(str1,'volatile'); % get each position of "volatile"
k1 = strfind(str1,' '); % get each position of "volatile"
You want to find 2d space after 'volatile' position
[k2,ix] = sort([k k1]); % sort 'spaces' and 'volatile' positions
[~,ind] = sort(ix);
ii = ind(1:length(k)); % indices of 'volatile'
Read my explanations and ask questions
ANAND VISHAL
ANAND VISHAL 2020년 3월 23일
편집: ANAND VISHAL 2020년 3월 23일
@darova i need one help
once you go through with input and output file there are one minor change
/* Exported data definition */ I dont need ro only before data please chk 5 line of output
#pragma ghs section data = ".CALIB_RAM_DATA_SECTION"
I need to add only ro before data not in 5 line of output ie (/* Exported data definition */)
#pragma ghs section rodata = ".CALIB_RAM_DATA_SECTION"
please chk file u get to know.
Thanks
What about simple strrep?
str = 'sect data ROC_DATA data'
str1 = strrep(str,'data','rodata')
all words data will be rodata
ANAND VISHAL
ANAND VISHAL 2020년 3월 23일
no i dont need all data to rodata
/* Exported data definition */ I dont need ro only before data please chk 5 line of output
please chk input and output file.
where there is star of #pragma:
input : #pragma ghs section data = ".CALIB_RAM_DATA_SECTION"
output: #pragma ghs section rodata = ".CALIB_RAM_DATA_SECTION"
ANAND VISHAL
ANAND VISHAL 2020년 3월 23일
There is some problem in this script can u check plsss its urgent.
what about
str1 = strrep(str,'section data','section rodata')
ANAND VISHAL
ANAND VISHAL 2020년 3월 23일
that is not giving correct output
darova
darova 2020년 3월 23일
  • There is some problem in this script can u check plsss its urgent.
Show the error
ANAND VISHAL
ANAND VISHAL 2020년 3월 26일
편집: ANAND VISHAL 2020년 3월 26일
@darova thanks.
its working
ANAND VISHAL
ANAND VISHAL 2020년 4월 2일
Hi @darova
i try to installed MinGW-w64 Compiler in matlba 2018b of version MinGW GCC 6.3 but its through error
Error using mex
No supported compiler or SDK was found. For options, visit http://www.mathworks.com/support/compilers/R2014b/win64.html.
Walter Roberson
Walter Roberson 2020년 4월 2일
This Question is tagged as being about R2017b. You talk about installing MinGW from R2018b (a different version). Your error message shows that you tried that in R2014b, a third version yet.
MinGW was not supported at the time of R2014b.
For R2014b, you need SDK 7.1 (but that can be difficult to install if you are using Windows 10), or you need a Profession (**not** Community or Express) version of Visual Studios.
ANAND VISHAL
ANAND VISHAL 2020년 4월 2일
no for 2018b?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 2월 27일

1 개 추천

fopen the file for 'a+' permission, and without the t attribute. It is important that you do not use 't'
Now fseek() on the fid, specifying you want movement relative to beginning of file, and specifying exactly how many bytes to count from the beginning. For this purpose you must know whether the file is using CR+LF or only LF because those CR change the byte count. The count is not the number of visible characters, it is the number of bytes. There is no way to seek by "lines" in a variable length line file: you seek by bytes. (There is a technical exception that is pretty restrictive.)
Once you have used fseek() to move to a byte position in the file that you opened with 'a+', you can proceed to fwrite() to replace bytes in the file.
Notice this is strictly replacement. There is no way to insert bytes. Therefore the only way to insert the Rom_ prefix is to replace every byte to the end of file.
If at some point you want to stop replacing bytes and move elsewhere in the file you must fseek().
If you fread() or similar some bytes and you reach end of file, then before you can write any bytes past the end of file, you must fseek(). Even if the fseek() says to move 0 bytes relative to where you are, the fseek() must be done between reading and writing. The fseek is important for internal buffer management including proper handling of cache and of the end of file flag.
So.. That is how you do what you asked for. It will not do what you want.
To do what you want, read in the entire file as one character vector, and use routines such as strrep or regexprep to produce a modified version of it in memory, after which you fopen 'w' (not 'wt') and fwrite() the memory buffer to file.

댓글 수: 2

ANAND VISHAL
ANAND VISHAL 2020년 2월 27일
it's not working. Can you snippet the code...
in_filename = 'Whatever.c';
out_filename = ['Rom_', in_filename];
S = fileread(in_filename);
newS = regexprep(S, '(volatile\s+\w+\s+)', '$1Rom_');
fid = fopen(out_filename, 'w');
fwrite(fid, newS);
fclose(fid)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2020년 2월 26일

댓글:

2020년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by