필터 지우기
필터 지우기

Write in a file, one ligne before the last one

조회 수: 3 (최근 30일)
Khalala Mamouri
Khalala Mamouri 2020년 8월 31일
댓글: Image Analyst 2020년 9월 2일
Hi
So i have a function file, and i want to write inside the file. ( sounds weired i know)
but i want toto write one ligne before the last line in the function script.
i made up this eexemole to explaine :
function [out_1, out_2] = test (in_1, )in_2
stuf ... % thethe main code of the function
end
i am using this code toto write inside the function file
file = fopen('test.m','a+')
fprintf(file,'%c','stuf')
fclose(file)
the idea is i want to write before the "end" of the function.
is there a way to Go back 3 caracters or up one ligne to sstart writing?

채택된 답변

Image Analyst
Image Analyst 2020년 9월 1일
Yes, open it, read all the lines, counting them in a loop. Then have a second loop reading from the input file, and writing to the output file. When the line number is the count minus 1, call your fprintf() to the output file. Then you can close both files and delete the input file and rename your output file using movefile() if you want. Easy, but let me know if you can't figure it out.
  댓글 수: 2
Khalala Mamouri
Khalala Mamouri 2020년 9월 1일
편집: Khalala Mamouri 2020년 9월 1일
Hi, This is the working code :
this is the code i am using to transfer data from text to text1
fid_in = fopen('Test.txt');
g = textscan(fid_in,'%s','delimiter','\n'); % Count N of lines
N_lines = length(g{1}); %
frewind(fid_in); % Re initialize fgetl
fid_out = fopen( 'test2.txt', 'w' );
lol = fgetl(fid_in); % Copiying data to the second file
for i = 1:N_lines-1
fprintf( fid_out, '%s \n', lol );
lol = fgetl(fid_in);
end
new_output = 'Henry the IXth '; % Add a line N
fprintf( fid_out, '%s\n', new_output );
fclose( fid_out );
fclose( fid_in ) ; % close all that things
%delete('Test.txt'); % Delete old file
rename = strcat(f,'Hello',txt) ; % << How to rename the file ?
So at the end we need to get that :
test.txt test1.txt
Orange Orange
Blue Blue
Green Henry the IXth I am
I am not sure how to do to rename the file ? can you please help me ?
Image Analyst
Image Analyst 2020년 9월 2일
Add this robust code:
originalFileName = fullfile(pwd, 'Test.txt');
newFileName = fullfile(pwd, 'Test2.txt');
if isfile(originalFileName) && isfile(newFileName)
recycle('on') % Make sure deleted file goes into recycle bin.
delete(originalFileName); % Delete old one.
movefile(newFileName, originalFileName); % Rename new one.
end

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

추가 답변 (1개)

Mario Malic
Mario Malic 2020년 9월 1일
편집: Mario Malic 2020년 9월 1일
With this, you'll get contents of the file in File_Data which is a cell size (ii,1) with character arrays.
FID = fopen('test.txt', 'r');
ii = 1;
tline = fgetl(FID);
File_Data{ii} = tline;
while ischar(tline)
ii = ii+1;
tline = fgetl(FID);
File_Data{ii} = tline;
end
fclose(FID);
Replacing the second to last line
File_Data{numel(File_Data)-1} = 'Henry the IXth I am';
You can delete the previous file, I am not sure if fopen would overwrite the file if it has the same name.
Write the file with
FID = fopen('test1.txt','wt'); %
for p = 1:numel(File_Data)
if File_Data{p+1} == -1 % -1 denotes the end of file
fprintf(FID,'%s', File_Data{p});
break
else
fprintf(FID,'%s\n', File_Data{p});
end
end
fclose(FID);
  댓글 수: 4
Khalala Mamouri
Khalala Mamouri 2020년 9월 1일
To be honest i did not understand. So i want to change the name of a file, lets say : Old_Name.txt to New_name.txt. How is it done ? thank you
Mario Malic
Mario Malic 2020년 9월 1일
You changed the order of files
From the documentation:
movefile source destination
%
movefile 'old.txt' 'new.txt'

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

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by