Q:

A data file called “mathfile.dat” stores three characters on each line: an operand (a single digit number), an operator (a one character operator, such as +, -, /, \\, *, ^), and then another operand (a single digit number). For example, it might look like this:

0

A data file called “mathfile.dat” stores three characters on each 

line: an operand (a single digit number), an operator (a one character 

operator, such as +, -, /, \, *, ^), and then another operand (a 

single digit number). For example, it might look like this:

>> type mathfile.dat

5+2

8-1

3+3

You are to write a script that will use fgetl to read from the file, one 

line at a time, perform the specified operation, and print the result.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ch9Ex16.m

% Read in math operations from a data file,

% perform the operation and print the result

fid = fopen('mathfile.dat');

if fid == -1

 disp('File open not successful')

else

 while feof(fid) == 0

 % Get operand, operator, operand

 % and perform the operation!

 aline = fgetl(fid);

 res = eval(aline);

 fprintf('%s = %d\n', aline, res)

 end

 closeresult = fclose(fid);

 if closeresult == 0

 disp('File close successful')

 else

 disp('File close not successful')

 end 

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now