how to modify input-output script to show how many line were copied.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Davifus
am 7 Nov. 2019
Beantwortet: Shubham Gupta
am 8 Nov. 2019
ifn = input( 'input file name: ', 's' );
ofn = input('output file name: ', 's' );
ih = fopen( ifn, 'r' );
oh = fopen( ofn, 'w' );
ln = '';
while ischar( ln )
ln = fgets( ih );
if ischar( ln )
fprintf( oh, ln );
end
end
fclose( ih );
fclose( oh );
So running the script creates another .txt file of the same content as the input file. How do I change the script so it will print out the # of line it copied?
0 Kommentare
Akzeptierte Antwort
Shubham Gupta
am 8 Nov. 2019
Try:
ifn = input( 'input file name: ', 's' );
ofn = input('output file name: ', 's' );
ih = fopen( ifn, 'r' );
oh = fopen( ofn, 'w' );
ln = '';
count = 0;
while ischar( ln )
ln = fgets( ih );
if ischar( ln )
count = count + 1;
fprintf( oh, ln );
fprintf('Number of line(s) copied = %d\n',count) % edited line
end
end
fclose( ih );
fclose( oh );
If you want only final count of line it printed bring the 'edited line' outside the while loop. Let me know if you have doubts !
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!