Q:

Create a file that has some college department names and enrollments. For example, it might look like this:

0

Aerospace 201

Mechanical 66

Write a script that will read the information from this file and create a 

new file that has just the first four characters from the department 

names, followed by the enrollments. The new file will be in this form:

Aero 201

Mech 66

All Answers

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

Ch9Ex28.m

% Read in department names and enrollments from one file and

% write first 4 chars of names w enrollments to another file

fid = fopen('eng.dat');

if fid == -1

 disp('File open not successful')

else

 %Open a new file for writing

 nfid = fopen('neweng.dat','w');

 while feof(fid) == 0

 aline = fgetl(fid);

 %Seprate department names and enrollment

 [dep num] = strtok(aline); 

 num = str2num(num);

 fprintf(nfid,'%s %d\n',dep(1:4),num); %Write to file

 end

 

 %Error-check file close

 closeresult = fclose('all');

 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