0
votes

I am writing a Matlab function that does some file manipulation as follows. I take the input file, read it line by line and write it to an output file. If the line contains a keyword, I do some further processing before writing the output. My problem is this: If the input line contains an escape character, it messes up the fprintf that I use to write the output. For example, if the input line contains a % sign, the rest of the line does not show up in the output. So, my question is: is there a way to force fprintf to ignore all escape sequences and print the literal string? Thanks in advance for the help.

Sample code below:

fptr_read = fopen('read_file.txt','r'); fptr_write = fopen('write_file.txt','w');
while(~feof(fptr_read))
  current_line = fgetl(fptr_read);
  fprintf(fptr_write,current_line);
end

If current_line looks like 'Gain is 5% larger', it will get written as 'Gain is 5'. I want the line reproduced verbatim without manually having to check for presence of escape characters.

1
Can you provide some sample code that has the bad behavior? And FWIW I had a similar problem once, and solved it like this fprintf(fp,'%s',stringWithEscapesIWant). - maxywb
This is exactly what I was looking for. With this construct, the string argument gets printed literally. Thanks a lot. - Dinesh
@Dinesh Don't forget to accept (and upvote?) the answer then - Luis Mendo

1 Answers

2
votes

I had a similar problem once, and solved it like this:

fprintf(fp,'%s',stringWithEscapesIWant)