Code Abbey cant find my data file

Back to General discussions forum

oldsparky     2016-02-15 22:24:52

When I do a Code Abbey exercise I copy the data to a file and read from it to do the exercise. When I paste my code into the code box on Code Abbey the data file I'm using is never found. I've tried @"FileName.txt" and I've also tried the full path @"F:\dir\filename.txt". What am I missing? By the way both methods work fine om my system. How can I get Code Abbey to find the data file?

b_pritom     2016-02-16 06:01:14

Is this what you are saying? You copy the test data to a file (say "file.txt"). The program reads this file and computes the answer(s). When you run your code from this site the solution doesn't work.

This happens because CodeAbbey (or the tool it's using) doesn't use your computer to compile the code. It doesn't know anything about a "file.txt"!

You can either manually copy and paste the answers from your console to the "Your answer" box of the site (without using the built in tools). Or, the easy way: You don't have to use a file at all! Use standard input to get the "Test data". For example in c++ use std::cin (or std::getline) and Console.ReadLine() in c# etc.

Matthew Cole     2016-02-16 06:57:05

What b pritom said is correct, but I'll expound just a bit.

If you're doing pretty much every problem on CodeAbbey, you need to read in input data from standard input. Here's how it works: Hackerrank - which powers CodeAbbey's solution checker - takes the contents of the test data block and passes it to your program via STDIN. You have two options:

  1. Hard code the data directly into your program as a global string constant. Then, write your program to process this string into useful input data. Looking at your most recent solutions, that appears to be what you're doing, so I won't go any further. Effectively, you're ignoring the input from STDIN; it's still there, you're just choosing not to do anything with it.
  2. Read the data from standard input. Since you're using C#, I'll steer you towards 3 methods: Console.Read, Console.ReadLine, and Console.OpenStandardInput. Here's a useful tutorial on using the Console class to capture inputs, then convert them to the correct type.

My advice: learn #2. #1 hard codes your program to a particular input, but #2 is dynamic and works with any input test data. You really need to learn this if you plan to continue programming.

To make it easy, here's a page on Hackerrank that shows exactly how to capture input in 14 different languages, including 6 out of 7 supported languages in CodeAbbey!

If you're doing a problem that requires you to read a file for input data (For example, Problem #141, Sliding Window Search or Problem #170, Binary Search in Array), then you have two options:

  1. Copy the file to your computer, run the solution on your computer and yank the results from your console/terminal, and finally paste the solution back into the "Your Answer" text box. Don't press the C# button in the "Code Running Tools" box, but do press "Submit" below the "Your Solution" text box.
  2. Read the file directly from the Code Abbey website using your code - no yank-and-paste from your console to the CodeAbbey HTML form required! Since you use C#, you might make use of the WebClient class. Here's a quick demonstration from StackOverflow of one way to do it. For other readers, your language can do this too! C and C++ have the LibCurl socket programming library, Python has the urllib module, Java has the URLConnection class. If you want to get really crazy, Haskell has the tagsoup module, Racket Lisp has the net/url library, even Prolog has the http_open/3 predicate!

My advice: if you're looking for quick and easy, then choose option #1. If you're looking to learn programming at a level that you'd need to know for a job interview or a career in programming, choose option #2.

oldsparky     2016-02-22 20:13:40

Thanks to you both. Option #2 it is.

Please login and solve 5 problems to be able to post at forum