필터 지우기
필터 지우기

How can I make a text file that I can edit after each time I run a program?

조회 수: 3 (최근 30일)
I want to print to a text file the name, score, and board size of the user once a user has completed a simple game I designed. I can do that task just fine, but I also want to display all of the previous users and their scores and board sizes as well and just add the most recent user to the list. If anyone can help that would be great.
this is what I have so far (there are values for Name, Score, and Size)
if true
fid=fopen('results.txt','w');
fprintf(fid, ['Name' '\t\t' 'Score' '\t' 'Board Size' '\n']);
fprintf(fid, '\n%s\t\t%d\t\t%d\n', Name, Score, Size');
fclose(fid);
end

채택된 답변

Abdelmoumen Bacetti
Abdelmoumen Bacetti 2015년 11월 29일
use the permission option 'a' which means append the data at the end of the file you can omit the first line so you don't repeat the header, unless this is what you want
so, assuming you have an old results file, do this to add new result at the end
if true
fid=fopen('results.txt','a');
fprintf(fid, '\n%s\t\t%d\t\t%d\n', Name, Score, Size');
fclose(fid);
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 11월 29일
fid = fopen('TheScoreFile.txt', 'at');
fprintf(fid, 'this line will be appended to the file\n');
fclose(fid)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by