Basic problems with accessing a file

Discussion in 'Ruby' started by James Calivar, Sep 7, 2006.

  1. Hello,

    I'm confused. I'm attempting to query a user for a filename to be
    processed, and then I want to open that file for parsing. However, I'm
    getting an error that the file does not exist. Hopefully someone can
    help?

    Here is my code:

    # Get filename
    puts ("Please enter the name of the file you wish to process: ")
    filename = gets
    puts ("File to be processed: " + filename)
    if File.exist?(filename)
    puts "Exists."
    else
    puts "Does not exist."
    end

    # Process file
    File.open(filename, "r") do |input_file|
    while line = input_file.gets
    puts line if line =~ /^[O] \[/
    end
    end

    Running this (Windows XP) produces the following:


    C:\Documents and Settings\jcalivar\Desktop>ruby
    RequirementsExtractor.rb
    Please enter the name of the file you wish to extract requirements
    from:
    fred.txt
    File to be processed: fred.txt
    Does not exist.
    RequirementsExtractor.rb:35:in `initialize': No such file or directory
    - fred.txt (Errno::ENOENT)
    from RequirementsExtractor.rb:35:in `open'
    from RequirementsExtractor.rb:35

    I can't figure this out - because "fred.txt" certainly *does* exist on
    my desktop. Help! :)

    James
     
    James Calivar, Sep 7, 2006
    #1
    1. Advertising

  2. James Calivar

    ts Guest

    >>>>> "J" == James Calivar <> writes:

    J> puts ("Please enter the name of the file you wish to process: ")
    J> filename = gets

    filename = gets.chomp # to remove the newline which is at the end of the
    # string

    J> puts ("File to be processed: " + filename)



    --

    Guy Decoux
     
    ts, Sep 7, 2006
    #2
    1. Advertising

  3. Is the gets giving you a string with a \n on the end?

    You might have to chomp it first.
     
    Peter Hickman, Sep 7, 2006
    #3
  4. I tried gets.chomp instead of gets and it worked! Thanks.

    James

    ts wrote:
    > >>>>> "J" == James Calivar <> writes:

    >
    > J> puts ("Please enter the name of the file you wish to process: ")
    > J> filename = gets
    >
    > filename = gets.chomp # to remove the newline which is at the end of the
    > # string
    >
    > J> puts ("File to be processed: " + filename)
    >
    >
    >
    > --
    >
    > Guy Decoux
     
    James Calivar, Sep 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Engineer
    Replies:
    6
    Views:
    673
    Jeremy Bowers
    May 1, 2005
  2. Replies:
    0
    Views:
    473
  3. Dominick Baier [DevelopMentor]

    401 Unauthorized when accessing basic Authentication site through

    Dominick Baier [DevelopMentor], Apr 3, 2006, in forum: ASP .Net Security
    Replies:
    2
    Views:
    234
  4. jswidorski
    Replies:
    2
    Views:
    153
    jswidorski
    Feb 18, 2005
  5. Bjoern Hoehrmann

    Problems accessing a XML-file

    Bjoern Hoehrmann, Jun 9, 2008, in forum: Javascript
    Replies:
    3
    Views:
    111
    Bart Van der Donck
    Jun 12, 2008
Loading...

Share This Page