Question

please help in matlab!!! Thank you so much in advance! Modify fn = input('input file name:',...

please help in matlab!!! Thank you so much in advance!

Modify

fn = 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 );

% fprintf( oh, ln ) has a paramter oh of file %handle.

Ln is written to the file instead of %printing on screen

  Please type the names of a source file and a target file. Then copy the content from source file to target file, and display how many lines are copied. For example, if you type ‘t1.txt’ and ‘t1_1.txt’ as source and target file names, the content of ‘t1.txt’ will be copied to ‘t1_1.txt,’ and 6 is displayed to show 6 lines are copied.

(Hint: copying was implemented in the example; you only need to add statements to calculate how many lines are copied.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

fgets read line from the file one at a time. So whenever we read a line, we can increment the count. The final count will be the number of lines in the file.

Here is the modified Matlab code that keeps track of lines and prints it at the end:

ifn = input('input file name:', 's' );

ofn = input('output file name: ','s' );

ih = fopen( ifn,'r');

oh = fopen( ofn,'w' );

ln = '';

newlines = 0; % initialize newline to zero

while ischar( ln )

   ln = fgets( ih );

   if ischar( ln )

       fprintf( oh, ln );

       newlines = newlines + 1; % increment newline

   end

end

fprintf("Total number of lines = %d", newlines); % print number of lines

fclose( ih );

fclose( oh );

Code screenshot:

Input file t1.txt:

Sample IO:

Add a comment
Know the answer?
Add Answer to:
please help in matlab!!! Thank you so much in advance! Modify fn = input('input file name:',...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • Please help with program this. Thank you so much in advance! Create a Java program which...

    Please help with program this. Thank you so much in advance! Create a Java program which implements a simple stack machine. The machine has 6 instructions Push operand Puts a value on the stack. The operand is either a floating point literal or one of 10 memory locations designated MO M9 Pop operand Pops the value on the top of the stack and moves it to the memory location MO-M9 Add Pops the top two values off the stack, performs...

  • Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements...

    Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements description: Assume you work part-time at a sandwich store. As the only employee who knows java programming, you help to write a sandwich ordering application for the store. The following is a brief requirement description with some sample output. 1. Selecting bread When the program starts, it first shows a list/menu of sandwich breads and their prices, then asks a user to select a...

  • The following are screen grabs of the provided files Thanks so much for your help, and have a n...

    The following are screen grabs of the provided files Thanks so much for your help, and have a nice day! My Java Programming Teacher Gave me this for practice before the exam, butI can't get it to work, and I need a working version to discuss with my teacher ASAP, and I would like to sleep at some point before the exam. Please Help TEST QUESTION 5: Tamagotchi For this question, you will write a number of classes that you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT