...
Du kan anta at ord er adskilt av mellomrom i filen.
Videoforklaring (
...
14:
...
30)
Del 1 (mm7:ss28) | Del 2 (mm7:ss02) |
---|
| |
|
---|
Widget Connector |
---|
url | https://www.youtube.com/watch?v=w_9f37sreNw |
---|
|
| Widget Connector |
---|
url | https://www.youtube.com/watch?v=ZAyMTdfa58M |
---|
|
|
Oppgaveintroduksjon, og kode for å håndtere lesing av tekst fra fil. | Kode som behandler teksten lest inn fra fil. | | |
Løsningsforslag
Expand |
---|
title | Hvis du har prøvd selv, trykk her for å se svaret... |
---|
|
Code Block |
---|
| function analyser_fil(filnavn)
% 1. Åpne filen
fd = fopen(filnavn, 'r');
if fd == -1
error('Kunne ikke åpne filen %s\n', filnavn);
end
% 2. Bruke filen
antall_linjer = 0;
antall_ord = 0;
while ~feof(fd)
linje = fgetl(fd);
% Behandle linjen
fprintf('%s\n', linje);
antall_linjer = antall_linjer + 1;
ord = strsplit(linje, ' ');
antall_ord = antall_ord + length(ord);
end
fprintf('Antall linjer: %d\n', antall_linjer);
fprintf('Antall ord: %d\n', antall_ord);
% 3. Lukke filen
status = fclose(fd);
if status == -1
error('Kunne ikke lukke filen %s\n', filnavn);
end
end |
|