2016-10-29 3 views
0

Ich versuche, ein Programm in Qbasic zu erstellen, in dem eine Person ihren Namen eingeben und sich als Administrator oder unerwünschter Benutzer bezeichnen kann. Wie speichere ich diese Einstellungen in meinem Programm?Wie speichere ich Namen in einer Qbasic-Datei?

+0

Ich habe versucht, den OPEN-Befehl, aber ich bin nicht in der Lage richtig zu nutzen. Auch ich nicht. verstehe die Logik hinter dem OPEN-Befehl. –

Antwort

1

Wenn Sie den Benutzernamen mit etwas inputed haben wie,

INPUT "Type your username: ", uName$ 

Um es in einer Datei zu speichern, einfach diese Befehle verwenden:

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

Hier ist ein komplettes Programm:

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

Um einen Namen in einer Datei zu speichern, müssen Sie die WRITE-Anweisung verwenden.
ZB:

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