2016-10-29 16 views
0

我想在Qbasic中創建一個程序,其中一個人可以輸入他們的名字並將自己標記爲管理員或不需要的用戶。我如何將這些偏好保存在我的程序中?如何將名稱保存在Qbasic文件中?

+0

我已經使用OPEN命令試過,但我不能夠正確地使用它。另外,我不知道。瞭解OPEN命令的邏輯。 –

回答

1

如果您inputed的東西,如用戶名,

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

要將名稱保存到文件中,您需要使用WRITE語句。
如:

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