如何在Qbasic文件中保存名称?

时间:2016-10-29 03:38:55

标签: qbasic

我正在尝试在Qbasic中创建一个程序,其中一个人可以输入他们的名字并将自己标记为管理员或不需要的用户。如何在程序中保存这些首选项?

2 个答案:

答案 0 :(得分:1)

如果您使用类似的内容输入了用户名,

INPUT "Type your username: ", uName$

要将其保存到文件中,只需使用以下命令:

OPEN "User.dat" FOR OUTPUT AS #1
  PRINT #1, uName$
CLOSE #1

这是一个完整的计划:

DEFINT A-Z

'Error handler for the first time we run the program.  The data file won't exist, so we create it.
ON ERROR GOTO FileNotExist

'Create a type and an Array of users that would include Username and the Status (adminstrator vs. Unwanted user)
TYPE user
  Uname AS STRING * 16
  Status AS STRING * 1
END TYPE

DIM Users(1 TO 100) AS user

'Gets all the users stored in the file.  i is a variable which represents the number of users before adding a new user
i = 0

OPEN "User.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
  i = i + 1
  INPUT #1, Users(i).Uname
  INPUT #1, Users(i).Status
WEND
CLOSE #1


TryAgain:

'Gets info for the new user
CLS
INPUT "User name: ", Users(i + 1).Uname
PRINT "Admin (a), Unwanted user (u), or Regular user (r) ?"
Users(i + 1).Status = LCASE$(INPUT$(1))

'Ensure there are no blank lines in the file
IF Users(i + 1).Uname = "" OR Users(i + 1).Status = "" THEN GOTO TryAgain


'Outputs user data to the file "User.txt"
OPEN "User.txt" FOR OUTPUT AS #1
  FOR j = 1 TO i + 1
    PRINT #1, Users(j).Uname
    PRINT #1, Users(j).Status
  NEXT j
CLOSE #1


'Just for a closer: Prints all the current users.
CLS
FOR j = 1 TO i + 1
  PRINT Users(j).Uname,
  IF Users(j).Status = "a" THEN PRINT "Amdinistrator" ELSE IF Users(j).Status = "u" THEN PRINT "Unwanted User" ELSE IF Users(j).Status = "r" THEN PRINT "Regular user" ELSE PRINT Users(j).Status
NEXT j
END



'*** ERROR HANDLER: ***

FileNotExist:        
OPEN "User.txt" FOR OUTPUT AS #1
CLOSE
RESUME

答案 1 :(得分:1)

要将名称保存到文件中,您需要使用WRITE声明 例如:

OPEN "Name.txt" FOR OUTPUT AS #1
INPUT"Enter a name";a$
WRITE #1,a$
CLOSE #1
END