Is it possible for Eclipse to read stdin from a file?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
For linux: I think, the easiest way is to write an sh-script.
1) write run.sh like this:
your program with params < input.txt
2) In the Eclipse profile configuration specify
[tab Main] C++ Run Application = /bin/sh
[tab Arguments] Program Arguments = run.sh
That's it. When you will run your project, you will see the script's output in the eclipse console. You can do something similar in Windows.
You will need to tweak your code some to get this working within eclipse. The other answers above did not work when I tried. Another I've seen saying to change Run..>Common tab>Standard Input and Output>File only changed the the stdout behavior to the file, not the input.
The solution was to take the "-d" option to a workable solution. If you put anything in the program arguments it's just going to be passed into your main. So what you can do at the beginning of main is something like this:
You will still need to set your program arguments in the Run.. menu as described in this answer.
Pure Java
You can redirect System.in with a single line of code:
See System.setIn().
Eclipse config
In Eclipse 4.5 or later, the launch configuration dialog can set System.in to read from a file. See the announcement here.
[Update] As it has been pointed out in the comments, this answer was misleading so I have updated it to correct the errors. [/Update]
On bash or command prompt you can do:
C:\myprogramm < file.txt
(Windows) or./myprogramm < file.txt
(Linux)Unfortunately in Eclipse it is not possible to achieve the same result, since there is no option to bind the stdin to a file in eclipse. Instead you will have to manually open a file stream in your code, and then read from it instead. One way to do this is by using a program argument to enable it and another one with the file parameter. See Scott's answer on what kind of code you need to add to parse the -d option from the program arguments array.
When you want to point to some file inside your Eclipse Project, you need to be careful to get the location right and use the ${resource_loc:} variable:
of course you can also put an absolute path:
The resource_loc parameter refers to your workspace. That is the folder where all you eclipse projects are stored in. From there you still have to reference your project folder and then the file that you want to load.
You may have to tweak the slash direction, depending if you use Linux or Winodws.
What I did was to create an Ant target and launch it as "Run External" from Eclipse, here are the steps:
res\in.txt
and one for the output:res\out.txt
Create a
build.xml
with the targets you require (this is just an example):In Eclipse go to:
Run->External Tools->External Tools Configurations->Ant Build-> New Launch Configuration
use the following configuration:Section Main
Buildfile:
${workspace_loc:/Tests/build.xml}
Base Directory:
${workspace_loc:/Tests}
*Note: Tests is the name of my Eclipse project
Now you can run your project by clicking in the Run Extenal tool bar button and change the input or output files as you needed
in debug configuration/arguments/program arguments:
works well for me.