Save information in the file
Write a program thatStudent Name and NumberReceive users and in a file namedStudents.txtSave. Also, after each storage, display the entire information of this file at the output.
Write a program thatStudent Name and NumberReceive users and in a file namedStudents.txtSave. Also, after each storage, display the entire information of this file at the output.
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
const { error, log } = require("console"); const { appendFile, readFile } = require("fs"); const { createInterface } = require("readline"); const rl = createInterface({ input: process.stdin, output: process.stdout, }); function display() { readFile("students.txt", "utf8", (err, data) => { if (err) { error("Error reading file:", err); } else { log("\nCurrent students:"); log(data); } promptUser(); }); } function save(name, id) { const data = `${name}:${id}\n`; appendFile("students.txt", data, (err) => { if (err) { error("Error saving data:", err); } else { log(`Saved: ${name} (ID: ${id})`); display(); } }); } function promptUser() { rl.question("Enter student name (or 'exit' to quit): ", (name) => { if (name.toLowerCase() === "exit") return rl.close(); rl.question("Enter student ID: ", (id) => { save(name, id); }); }); } appendFile("students.txt", "", (err) => { if (err) { error("Error creating file:", err); } else { log("Welcome to the Student Management System"); promptUser(); } }); rl.on("close", () => { log("Thank you for using the Student Management System. Goodbye!"); process.exit(0); });
hile 1:
name = input("Enter your name: ")
if name == "": break
student_number = input("Enter your student number: ")
file = open("students.txt", "a")
file.write(f'Name: {name}\nStudent Number: {student_number}\n')
file.close()
file = open("students.txt", "r")
print(file.read())
file.close()
while 1: name, shomare_daneshjooei = input("nam khod ra vared konid :"), input("shomare daneshjooei ra vared konid :") if name == "": break hasel = f'###\nNam :{name}\nshomare : {shomare_daneshjooei}\n' save = open("Etelaat.txt", "a") save.write(hasel) save.close() namayesh = open("Etelaat.txt", 'r') print(namayesh.read()) namayesh.close()
package org.example import com.google.gson.Gson import com.google.gson.reflect.TypeToken import java.io.File import java.lang.reflect.Type fun main() { val map1 = mutableMapOf<String, String>() val file = File("D:\\New folder (5)\\students.txt") var finish :String while (true) { try { when (menu1()) { 1 -> { finish=gsonAppend(menu2(map1),file) map1.clear() map1.putAll(toSaved(finish,map1)) println("information is saved") } 2 -> { showing(file) } 0 -> {println("Program is Exist") break } } } catch (e: Exception) { println(e.message) } } } fun menu1(): Int { println("Please Enter one number that is want:") println(" Enter:[1]->To register student name and student number") println(" Enter:[2]->To show the list of student names along with the student number") println(" Enter:[0]->To Exit of Program") val number = readlnOrNull()?.toInt()!! return number } fun menu2(map1:MutableMap<String,String>): Pair<String, String> { println("Please Enter Student name") val name = readlnOrNull()!! println("Please Enter Student number") val number: String = readlnOrNull()!! return map1.let { (name to number) } } fun gsonAppend(map1: Pair<String, String>,file:File):String { val gson = Gson() val map2 = gson.toJson(map1) file.appendText(map2) return map2 } fun toSaved(string1:String,map1:MutableMap<String,String>):MutableMap<String,String>{ val gson = Gson() val mutableMapProduct: Type = object : TypeToken<MutableMap<String,String>>() {}.type val result = gson.fromJson<MutableMap<String,String>>(string1,mutableMapProduct ) map1.clear() map1.putAll(result) return map1 } fun showing(file:File){ file.forEachLine { println(it) } }
def save_student_info(): while True: # Get information from the user name = input("اسم دانشجو را وارد کنید (برای خروج، 'exit' را وارد کنید): ") if name.lower() == 'exit': # Check to exit break student_id = input("شماره دانشجویی را وارد کنید: ") # Save information in the file with open("students.txt", "a") as file: # Open the file to add mode file.write(f"{name}, {student_id}\n") # Write information in the file # Reading and displaying the entire file information with open("students.txt", "r") as file: # Open the file to read mode print("\nکل اطلاعات ذخیره شده در فایل:") for line in file: print(line.strip()) # Display any line without additional distances # Implementation of the function save_student_info()
def add_student_info(): # Get a student name and number of user name = input("نام دانشجو را وارد کنید: ") student_id = input("شماره دانشجویی را وارد کنید: ") # Open the file as Append and save information with open("students.txt", "a") as file: file.write(f"{name}, {student_id}\n") # Reading and displaying the entire file information with open("students.txt", "r") as file: content = file.read() print("\nمحتویات فایل students.txt:\n") print(content)
stu_file=open('D:\\student.txt','a') while 1: name=input("Name : ") if name=='':break password=input("password : ") if password=='':break stu_file.write(f"name : {name} - password : {password}\n") stu_file.close() stu_file=open('D:\\student.txt','r') print(stu_file.read()) stu_file.close()
Submitting answers is currently unavailable.
Write a program that receives the user's name, age and city and print on output
Write a function that receives a string and converts Persian numbers into English numbers
You must be logged in to access this section.
Login/Sign up If you don’t understand the exercise or can’t solve it for any reason, that’s completely
normal—don’t worry 😊
Try checking out easier exercises and reviewing different answers
submitted by others. Gradually, you can move on to more challenging exercises. Also, your answer
might be correct even if it’s different from others.