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 天)
显示 更早的评论
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!"
0 个评论
回答(1 个)
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);
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!