how to write an opened file in a loop to a new file in a different folder using fwrite?

조회 수: 8 (최근 30일)
Hi everyone,
I open a file in a loop using fopen. I make some modifications and then I want to write the file to a new folder but I get the error that the identifier is not valid.
Loop starts >
OpenFile = fopen('BetaWeatherFile.epw','r');
DuplicateATemporary = fopen('temporary.epw','w');
.....
modifications
.....
Then I want to use the fwrite here but it does not work.
"Error using fwrite Invalid file identifier. Use fopen to generate a valid file identifier."
  댓글 수: 6
Wolfgang McCormack
Wolfgang McCormack 2021년 3월 10일
@Mohammad Sami @Jorg Woehl funny story guys, I tried to make it as simple as possible (even getting rid of the dublicate) using the code you suggested. FileOpen goes right to 3! no 1 no 2 neither -1 :D
clear all; clc
OpenFile = fopen('C:\example\BetaWeatherFile.epw','r')
assert(OpenFile~=-1, 'Cannot open BetaWeatherFile.epw.')
fwrite(OpenFile, 'E:\backup.epw')
fclose('all')
This is what I get:
OpenFile =
3
ans =
0
ans =
0
OpenFile is an array that includes number 3.
Jan
Jan 2021년 3월 10일
Yes, of course the first valid file identifier is 3. 1 is the standard output, 2 the channel for error messages.
With opening the file for reading by 'r', you cannot write to the file. Therefor your fwrite command replies 0 for: no character was written.
Are you sure you want to write the char vector 'E:\backup.epw' into the file? Maybe you have confused FWRITE and COPYFILE?

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

채택된 답변

Jan
Jan 2021년 3월 10일
편집: Jan 2021년 3월 11일
Yes, of course the first valid file identifier is 3. 1 is the standard output, 2 the channel for error messages.
With opening the file for reading by 'r', you cannot write to the file. Therefor your fwrite command replies 0 for: no character was written.
Are you sure you want to write the char vector 'E:\backup.epw' into the file? Maybe you have confused FWRITE and COPYFILE?
FileName = fullfile(tempdir, 'test.txt')
OpenFile = fopen(FileName, 'w'); % [EDITED, typo fixed]
assert(OpenFile~=-1, 'Cannot open file for writing.')
fwrite(OpenFile, 'Hi', 'char')
fclose(OpenFile);
copyfile(FileName, fullfile(tempdir, 'test2.txt'))
  댓글 수: 4
Wolfgang McCormack
Wolfgang McCormack 2021년 3월 10일
@Jan nice, your code gave me the hint to get the job done (my matlab treats that 'w' as part of the file directory). So now another question. This is happening within a loop and every time a 'test.txt' is being written (overwritten). How should i use this copyfile to get a copy of each temp file; will this work?
CopyPhase(i) = copyfile(Thefileis, fullfile(tempdir, 'test.txt'))?
or should I use the i within the name of test.txt?
Jan
Jan 2021년 3월 11일
What do you expect as output of the copyfile command? It is a flag, which tells you if the copy was successful. I do not see a reason to store this in the variable CopyPhase .
Instead of copying the file, you could write directly to the wanted file. Then using an index in the file name is an obvious solution:
for k = 1:10
file = fullfile(Folder, sprintf('file%03d.txt', k));
...
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by