0

Can't seem to get my data to be read as an integer and print out the data. plus there is a close stream (IOError) for count = aFile.gets in def read (afile) function. This program includes Array, files and loops. The purpose of this program is to take a number 10 and write the number to a file then on each line increment from zero to 10 that is passed.

# takes a number and writes that number to a file then on each line
# increments from zero to the number passed
def write(aFile, number)
  # You might need to fix this next line:
  aFile.puts("number")
  index = 0
  while (index < number)
   aFile.puts(number.to_s)
   index += 1
  end
end

# Read the data from the file and print out each line
def read(aFile)

  # Defensive programming:
  count = aFile.gets
  if (is_numeric(count))
    count = count.to_i
  else
    count = 0
    puts "Error: first line of file is not a number"
  end

  index = 0
  while (count < index)
    line = aFile.gets
    puts "Line read: " + line
  end
end

# Write data to a file then read it in and print it out
def main
  aFile = File.new("mydata.txt", "w") # open for writing
  if aFile  # if nil this test will be false
    write(aFile, 10)
  else
    puts "Unable to open file to write!"
  end

  if aFile
    read(aFile)
  end
  aFile.close
end

# returns true if a string contains only digits
def is_numeric?(obj)
  if /[^0-9]/.match(obj) == nil
    true
  end
  false
end

main

Elham
  • 11
  • 3
  • Welcome to SO! "The purpose of this program" sounds suspiciously like you're doing a homework assignment. If so, please read "[How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions)". – the Tin Man May 30 '19 at 23:08
  • You're creating methods `read` and `write` that are named the same as very commonly used IO and File methods. I'd recommend not doing that because you'll confuse your future self or coworkers. In Ruby we don't use snake case for variables like `aFile`, we use `a_file`. – the Tin Man May 30 '19 at 23:22

2 Answers2

0

Here is a skinned code working, you'd easily add the features I removed.

def write(a_file, number)
  (1..number).each { |n| a_file.puts(n) }
end

def read(a_file)
  a_file.each  { |line| puts line }
end

def main
  a_file = File.new("mydata.txt", "w")
  if a_file
    write(a_file, 10)
  else
    puts "Unable to open file to write!"
  end
  a_file.close

  a_file = File.open("mydata.txt", "r")
  if a_file
    read(a_file)
  end
  a_file.close
end

main

The main bugs I've found:

Side note: use Ruby notation for variables: a_file is good, aFile is not.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
iGian
  • 11,023
  • 3
  • 21
  • 36
0

If you want to make your code work, change:

aFile = File.new("mydata.txt", "w")

to:

aFile = File.new("mydata.txt", "r+")

You can change:

 count = aFile.gets
 if (is_numeric(count))

to:

  count = aFile.gets.to_i
 if (count.is_a?(Fixnum))

and then get rid of the is_numeric?(obj) method.

Also you're not incrementing the counter, you can fix that as well.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
SgtPepper
  • 418
  • 6
  • 18