4

I've read many answers about this question but nothing was found about the comparison between two files, actually this is a sample of the book Algorithms based on BinarySearch, here is the source code

import java.util.Arrays;
import edu.princeton.cs.algs4.*;

public class prac1_1_23{

public static boolean BinaryLookup(int key, int[] arr) {
    int low = 0;
    int high = arr.length - 1;
    while(low <= high) {
        int mid = low + ((high - low) >> 1);
        if(key < arr[mid])
            high = mid - 1;
        else if(key > arr[mid])
            low = mid + 1;
        else
            return true;
    }
    return false;
}

public static void main(String[] args) {
    char symbol = '-';
    int[] whitelist = new In(args[0]).readAllInts();
    Arrays.sort(whitelist);
    while(!StdIn.isEmpty()) {
        int key = StdIn.readInt();
        boolean found = BinaryLookup(key, whitelist);
        if('+' == symbol && !found)
            StdOut.println(key);
        if('-' == symbol && found)
            StdOut.println(key);
    }
}
}

This sample utilizes a library made by the author of the book, which can be accessed via Algorithms, and the question is when I want to run this program via the PowerShell of windows,like the command

java prac1_1_23 largeW.txt < largeT.txt

I got a problem like error

actually I find a solution to run this code but is useless to solve it on PowerShell which requires me to use the commandline program written by the author of this book which can be download on the website of "algs4.cs.princeton.edu/windows/", and it need to compile and run the program with the commandline like

javac-algs4 prac1_1_23.java    //compile command

java-algs4 prac1_1_23 largeW.txt < largeT.txt   //run command

it does work but I wonder if we can utilize the original CLI because I found someone can run the original code on the Linux operating system without problems.

Any help is appreciated, thank you.

LancelotHolmes
  • 659
  • 1
  • 10
  • 31
  • what do u need exactly?witch parameter of file you need to compare together? size length ... – saftargholi Sep 15 '16 at 05:37
  • sorry to confuse you, actually I want to compare file **largeW.txt** and file **largeT.txt**, as is shown in the code, the **largeW.txt** is read via _args[0]_ of main as the _whitelist_ and the **largeT.txt** is read through the _StdIn.readInt()_ method as key in the while loop, thanks for your time – LancelotHolmes Sep 15 '16 at 06:03
  • can u give me an example with input and output? – saftargholi Sep 15 '16 at 06:06
  • The data are all big random numbers like 944443 293674 572153 600579 499569 984875 763178 295754 ......which you can get it on the website of http://algs4.cs.princeton.edu/code/, but I only find the link to download the all data needed for the book, and @default locale has offered me the answer, anyway, thanks for your help~ – LancelotHolmes Sep 15 '16 at 06:36

2 Answers2

14

Have you tried prefixing the redirection with the --% operator? For example:

   cmd /c --% java prac1_1_23 largeW.txt < largeT.txt

The command above prefixes your command with three things, let me explain them:

  • cmd invokes cmd.exe, which knows what you mean by <

  • /c tells cmd.exe to process one command following on the command line and then exit.

  • --% tells PowerShell to leave the rest of the command line alone, so that cmd.exe can deal with the < redirection.

This way you don't need a command script.

Knowledge Craving
  • 7,955
  • 13
  • 49
  • 92
Burt_Harris
  • 6,415
  • 2
  • 29
  • 64
  • Thank You, I 've tried both two commands, and the second one works well, is it means execute in cmd? sorry I'm quite new to those field if I have asked a stupid question. – LancelotHolmes Sep 16 '16 at 01:40
  • That is a perfectly good question. I have updated my answer to explain and remove the one that didn't work.. – Burt_Harris Sep 16 '16 at 02:52
  • Nice use case for verbatim parameter. You might want to post your answer to [this similar question](http://stackoverflow.com/questions/2148746/the-operator-is-reserved-for-future-use-powershell) as well. – default locale Sep 16 '16 at 03:21
  • It's very nice of you to give such further explanation,thank you again – LancelotHolmes Sep 16 '16 at 04:39
3

This is a powershell issue as explained in The '<' operator is reserved for future use (PowerShell).

As explained in the first answer you can run your command like this;

Get-Content largeT.txt | java prac1_1_23 largeW.txt

Check out other answers for alternative ways to redirect input in powershell.

Community
  • 1
  • 1
default locale
  • 13,035
  • 13
  • 56
  • 62
  • Well, thank you, actually I have tried this method but my powershell just corrupt and finally I change the file to small files like **tinyW.txt** and **tinyT.txt** and it does work, thanks for your help – LancelotHolmes Sep 15 '16 at 06:11
  • @LancelotHolmes also, you can put your command `java prac1_1_23 largeW.txt < largeT.txt` in a bat file as explained [here](http://stackoverflow.com/a/16998002/451518). By the way is there a specific reason to run java from powershell and not cmd, for example? – default locale Sep 15 '16 at 06:14
  • Sorry, I thought that the powershell is more convinient than cmd which brings me such a problem, anyway, it's very nice of you to give me so much helpful information, thank you again! – LancelotHolmes Sep 15 '16 at 06:27
  • 1
    Using Get-Content and piping the result to a native command doesn't work in some cases, I think it's because it ends up messing with the Unicode encoding the native program receives. – Burt_Harris Sep 16 '16 at 03:06