# This program creates a file, adds data to the file, displays the file,
# appends more data to the file, displays the file, and then deletes the file.
# It will not run if the file already exists.

def create_file(filename)
    file = File.new(filename, "w")
    file.puts("C\tF")
    for c in (0..50).step(1)
        f = c * 9.0 / 5.0 + 32
        file.puts (c).to_s + "\t" + (f).to_s
    end
    file.close
end

def read_file(filename)
    file = File.open(filename, "r")
    while not file.eof
        line = file.gets
        puts(line)
    end
    file.close
    puts("")
end

def append_file(filename)
    file = File.open(filename, "a")
    for c in (51..100).step(1)
        f = c * 9.0 / 5.0 + 32
        file.puts (c).to_s + "\t" + (f).to_s
    end
    file.close
end

def delete_file(filename)
    File.delete(filename)
end

def main()
    filename = "~file.txt"

    if File.exist?(filename)
        print("File already exists.")
    else
        create_file(filename)
        read_file(filename)
        append_file(filename)
        read_file(filename)
        delete_file(filename)
    end
end
    
main()

कोशिश करो

सम्पादन

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें

सम्पादन