Write data to text file containing \s in string?
Show older comments
Hi,
I'm writing a code that change some data for some lines in a text file. However, a line that is not changed (only taken from first file and written to the second text file) contains some backslashes (search path). I get a warning about this and the text sting containing this path is not fully written. I can not solve this. Can someone help me?
Warning: Control Character '\s' is not valid. See 'doc sprintf' for
control characters valid in the format string.
CODE:
textfil = fopen('C:\Stltmp2D\Dat\TEMP.dat', 'r');
textfil2 = fopen('C:\Stltmp2D\Dat\TEMP_UT.dat', 'w');
%
tline = fgetl(textfil);
%
k =1;
while ischar(tline)
if strcmp(tline(1:2),'LX');
tline(15:20) = num2str(Lx(1),'%4.4f');
tline(25:30) = num2str(Lx(2),'%4.4f');
tline(35:40) = num2str(Lx(3),'%4.4f');
end
fprintf(textfil2,tline);
fprintf(textfil2,'\n'); %%????
tline = fgetl(textfil);
k = k+1;
end % END OF CODE
Best regards
Christopher
Accepted Answer
More Answers (1)
Replace
fprintf(textfil2, '\n')
by
fprintf(textfil2, '%s', '\n')
In the first case '\n' is interpreted as format specifier, while in the second case '%s' means to insert the argument as string. Alternatively:
fwrite(textfil2, '\n', 'char')
This does not interpret anything, but writes the string directly into the file.
The same problem occurs in error messages, which contain file names:
FileName = 'C:\valid\folder\%23xyz';
error('My:Error:ID', ['File access failed: ', FileName]); % FAIL
Better:
error('My:Error:ID', 'File access failed: %s', FileName);
3 Comments
Christopher Fallqvist
on 2 Mar 2018
Guillaume
on 2 Mar 2018
No!
fprintf(textfil2, '\n')
is interpreted correctly by matlab and will output char(10) to the file. It is not interpreted by matlab as a format string as it does not contain any format specifier (which require a %). Doing
fprintf(textfil2, '%s', '\n')
will actually output the literal string '\n' to the file, not char(10). Escape sequences are not interpreted in the inputs to the format string.
The problem was with the other line
fprintf(textfil2, tline)
As stated in the question tline could contain \, which if present in the second input argument are interpreted as escape character (just as it is for the '\n'). Fixing the actual problem only required:
fprintf(textfil2, '%s', tline);
fprintf(textfil2, '\n'); %didn' need to change
In my answer I just conflated the two lines
@Guillaume: As far as I understood, the OP wants the characters '\' and 'n' appear in the file:
However, a line that is not changed (only taken from first file
and written to the second text file) contains some backslashes
(search path).
The actual warning is:
Control Character '\s' is not valid.
So it is a problem of writing the backslash to the file and therefore the text mode is not relevant here.
You wrote:
The problem was with the other line
fprintf(textfil2, tline)
Yes, exactly. And this tline contains backslashes.
But my example might be confusing. All I wanted to emphasize is the difference between
fprintf(fid, '\n')
and
fprintf(fid, '%s', '\n')
Categories
Find more on Language Support in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!