#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    16
    Rep Power
    0

    Reading and Writing a file


    I have written a program that searches for prime numbers and then does 2 things 1)it writes them down 2) it puts them in an array that is used for checking other numbers. This leaves me with 2 problems. a)I have to keep checking the program to copy the given primes into a word document b)everytime I restart the program I have to start from 0 again as the array is empty.
    Is there a way I could have my program write the numbers to a file and then when the program is restarted take the numbers from that file and put them in the array.
    Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    I would suggest you just use Marshal to store the data to file.
    Code:
    irb(main):001:0> a = (1..10).map { |x| x**2 }
    => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    irb(main):002:0> f = Marshal.dump(a)
    => "\004\b[\017i\006i\ti\016i\025i\036i)i6iEiVii"
    irb(main):003:0> b = Marshal.load(f)
    => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    irb(main):004:0> a == b
    => true
    The Marshal.dump will take a second parameter (an IO object) if you want to write to file.
    Not only will this allow you to save the current state of your program on the local computer, but it allows you to load it up from any other computer and always get the proper data regardless of platform.
    Works for all Ruby objects so you can still use this idea if you decide to write your own classes instead of using the list built-in.

    Comments on this post

    • Arty Ziff agrees
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    16
    Rep Power
    0
    Thanks I think I understand how Marshal.dump works but my knowledge of ruby is weak enough that I don't actually know what an IO object is. Any further help on that front would be very helpfull.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Its just an object that support input/output operations.
    Code:
    irb(main):001:0> f = File.new("saved.array", "w")
    => #<File:saved.array>
    irb(main):002:0> a = [1,2,3]
    => [1, 2, 3]
    irb(main):003:0> Marshal.dump(a,f)
    => #<File:saved.array>
    irb(main):005:0> f.close
    => nil
    irb(main):006:0> m = File.open("saved.array")
    => #<File:saved.array>
    irb(main):007:0> b = Marshal.load(m)
    => [1, 2, 3]
    irb(main):008:0> a == b
    => true
    The above example creates a dump file, writes to it, and then reads from it again.
    Hope this helps.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    16
    Rep Power
    0
    Ok I got that but when I save to a file [1,2,3] comes out as random symbols and I want to be able to read these numbers. How can I do this. Thanks and sorry for the continuous problems.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    So you want a human-readable format?
    If that is the case, then open the file like shown above and do the following:
    Code:
    a = [1,2,3]
    a.each do |x|
       f.puts x
    end
    Of course, to read them you now need to read each line and assign it to a value...
    Code:
    f.each do |line|
       b << line.chomp.to_i
    end
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    16
    Rep Power
    0
    That makes sense however when I add that to my program it crashs when I try to write to the file and comes up with an error message:
    primenumber2.rb:35:in `write': not opened for writing (IOError)
    from primenumber2.rb:35:in `puts'
    from primenumber2.rb:35

    To make it a little easier for you I will post the code:
    Code:
    puts 'please enter the number of integers you would like to check'
    finish=gets.chomp.to_f
    
    
    timer=Time.now
    primes=[]
    
    p=File.open('primes')
    p.each do |line|
      primes<<line.chomp.to_f
    end
    
    start=primes.last+1
    finish=(finish+start).to_f
    
    puts
    puts
    puts 'the prime numbers are:'
    
    
    while start<finish
      divisornum=0
      divisor=primes[divisornum]
      primenumber=true
      while divisor.to_f<=start.to_f/2 and divisornum<primes.length and primenumber==true
        if start/divisor==(start/divisor).to_i
          primenumber=false
        end
        divisornum=divisornum+1
        divisor=primes[divisornum]
      end
      if primenumber==true
        puts start.to_i
        primes.push start
        p.puts start.to_i
      end
      start=start+1
    end
    
    p.close
    
    puts Time.now-timer
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Code:
    p=File.open('primes')
    p.each do |line|
      primes<<line.chomp.to_f
    end
    Here you open a file in the default mode (not for writing). Then you read from it and then try writing to it. You should either open the file in read/write mode or write to a new file once you are done.
    There are several ways to go about what you want; seeking to the beginning of a file, closing and reopening in write mode, closing and reopening in append mode.
    Look through the Ruby documentation for the IO class (or just File) to see all our options and a few examples.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    16
    Rep Power
    0
    Thank you for all your help despite my persistent problems. I managed to get it to work by adding
    Code:
    p.close
    p=File.open('primes', 'a')
    after the bit where it opened and read the file.
    I probably shouldn't have posted my whole code in my previous post and I'm sorry about that.
    Once again thanks for all your help.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Dont worry about posting that much code. It was manageable and allowed anyone willing to help to make an informed decision. As things get larger, you would only want to post a subset of the whole to help minimize noise, but at this point you're fine.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0
    hello.. you have a human-readable format?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    16
    Rep Power
    0
    Yes. Why?
    This thread is quite old, I only realised you had posted on it because I have subscribed to it.

    Comments on this post

    • Arty Ziff agrees : No, old is a thread from 2004. This thread is quite fresh.

IMN logo majestic logo threadwatch logo seochat tools logo