フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Program that can take three lines input by the user running the code, and saves those lines of text in a text file

1 回表示 (過去 30 日間)
Matthew Finney
Matthew Finney 2019 年 4 月 2 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Would someone be able to assist me in creating a rather simple program? It needs to take the user inputs of the following three lines from their input and then save it to a file t3.txt.
"Get started."
"Come on!"
"Way to go!"

回答 (1 件)

Bob Thompson
Bob Thompson 2019 年 4 月 2 日
There are a couple of different ways of doing this, mostly based on how much you trust the user to put things in correctly.
The first option is to use multiple user inputs, one for each line. You can even make this variable so the user can put in more than three lines if you would like.
nl = input('How many lines would you like to input? ');
str = '';
for i = 1:nl;
tmp = input('Please enter a line: ','s'); % Double check that 's' is the appropriate flag
str = [str,'\n\n',tmp];
end
fout = fopen('t3.txt','w');
fprintf(fout,str);
fclose(fout);
Alternatively, you can simply have the user input their own new line characters and then the string can be entered as a single input.
str = ('Please input all lines: ','s');
fout = fopen('t3.txt','w');
fprintf(fout,str);
fclose(fout);

この質問は閉じられています。

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by