redirection input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
fdaniii
Posts: 7
Joined: 16 Feb 2012 09:07

redirection input

#1 Post by fdaniii » 16 Feb 2012 09:33

Hi all,
i'm new....on this forum....
my problem is: i written a dos program that interfaces with a microcontroller.
The program has the following structure (in pseudocode):

initialize some parameters;
do {
execute some operations;
while(!input parameter is "q")

It repeat the code until the inserted parameter is not equal to "q".
The code works fine if launched on the command promt.

My question is: why if i call the dos program in this way : C:\>myprogram.exe<myinput.txt
(with the redirection input file)
the program terminates the execution also if in the myinput.txt file the "q" character is not present?

I have the necessity to change the file input.text from MATLAB and repeat the do-while when needed.
How to implement this? It is possible to keep the myprogram in wait of parameters from file? Or this is not feasible, beacose the program will terminate the execution when EOF is reached?

thank you in advance

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: redirection input

#2 Post by jeb » 16 Feb 2012 10:10

fdaniii wrote:My question is: why if i call the dos program in this way : C:\>myprogram.exe<myinput.txt
(with the redirection input file)
the program terminates the execution also if in the myinput.txt file the "q" character is not present?


Obviously there is a bug in your code :!:

hope it helps
jeb

fdaniii
Posts: 7
Joined: 16 Feb 2012 09:07

Re: redirection input

#3 Post by fdaniii » 16 Feb 2012 10:19

No bugs in my code... the program works fine if i call it normally from the command prompt
with c:\>myprogram.exe and pass the input parameters whne required... the program terminate only if i insert the "q" char .
The problem arise when i call the program with the redicetion input file... :?

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: redirection input

#4 Post by jeb » 16 Feb 2012 10:27

Good argumentation, as one case exists where your program works, you conclude that it is bug free :!:
So the bug must be in the microsoft redirections?

What do you expect what we can do?
Guessing that in lkine 17 your code can be optimized?
Or that the rediections can't work with your code, as your code in line 22 avoid this?

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: redirection input

#5 Post by Liviu » 16 Feb 2012 10:41

fdaniii wrote:No bugs in my code... the program works fine if i call it normally from the command prompt with c:\>myprogram.exe and pass the input parameters whne required... the program terminate only if i insert the "q" char .

What happens if you type-in Ctrl-Z interactively? What happens if you run "myprogram.exe <nul"? Jeb is right, this is a matter of how your program reads the input stream, and the problem must lie in the code that you haven't shown (and which is not batch code).

Liviu

fdaniii
Posts: 7
Joined: 16 Feb 2012 09:07

Re: redirection input

#6 Post by fdaniii » 16 Feb 2012 10:55

This is my program (simplified), it is written in java...

Code: Select all

public static void main(String [] args) {
   // Some initialization   

        InputStreamReader reader = new InputStreamReader (System.in);
   BufferedReader readInput= new BufferedReader (reader);
     
do { 
    // My istructions....
          Wath my program do

      System.out.print("\n\nReturn to restart (q->exit):  ");
      try { choise = readInput.readLine(); }
      catch (IOException e) { logger.error("Input error: " + e); }
   
    } while(!choise.equals("q"));
   
   //(contatore<1);
    System.out.print("...Correct end of program\n\n\n\n\n");
    logger.info("Correct end of program\n\n\n");
       
  } //end main 



My question is... is there a way (with a batch file) to read input file interactively?
I need to restar the while loop when an event in matlab occurs... and the only thing that i can do it is to write to a file something....

When i call the program with myprogram.exe<nul, the loop executes only once....

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: redirection input

#7 Post by Liviu » 16 Feb 2012 11:47

fdaniii wrote:This is my program (simplified), it is written in java...
...
My question is... is there a way (with a batch file) to read input file interactively?
...
When i call the program with myprogram.exe<nul, the loop executes only once....

Presumably, this means the program stops when the input stream is closed. Which makes sense, since once the input stream is closed it can't be reopened, and the program would hang waiting for user input that never could come.

And, sorry, I don't know what "read input file interactively" would mean. At this point, it looks more like a program design matter than a batch one, so this is probably not the best place to get advice on that.

Liviu

fdaniii
Posts: 7
Joined: 16 Feb 2012 09:07

Re: redirection input

#8 Post by fdaniii » 17 Feb 2012 03:35

i mean... is there a way to update the input stream? or... is there a way to keep opened the input stream?

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: redirection input

#9 Post by alan_b » 17 Feb 2012 04:26

A Java problem belongs on a Java Forum, not a DOS BATCH forum.

With many decades of software experience which excludes Java,
I GUESS your problem is you wrote the wrong things so it does not do what you intended.

You intended :-
the program terminate only if i insert the "q" char

You coded :-
System.out.print("\n\nReturn to restart (q->exit): ");

My guess is that "System.out" will cause a (q->exit) abort upon running out of input,
possibly achieving an abort with (q->exit) delivering "q" to the line
} while(!choise.equals("q"));

Incidentally, is that valid ?
It certainly does not look English

I Googled "while(!choise.equals"
4 results (0.34 seconds)
Did you mean: "while(!choice.equals"

Google felt the same as me

I Googled "while(!choice.equals"
About 5,180 results (0.15 seconds)


I make that a 1000 to 1 vote against the spelling in your code :cry:

Alan

fdaniii
Posts: 7
Joined: 16 Feb 2012 09:07

Re: redirection input

#10 Post by fdaniii » 17 Feb 2012 07:48

A Java problem belongs on a Java Forum, not a DOS BATCH forum.

It is not a java problem!!!!!! the program works fine whitout an input redirection file
(works fine if i enter the "q" key or "return" in the command line promt!!)!!!
If myinout.txt contains the following characters:

a
s
e
r
t
e
q
w
e
r

the program runs until q: it repeat the while loop 7 times and then terminates (correctly!!!).
But if the myinput.txt doesn't contain the "q" character as the following:

a
s
e
r
t

the program will terminate when the input character are finished
(or when the EOF is reached.. i don't know this... is one of the my questions!!)

I Googled "while(!choise.equals"


if you look inside the code... "choise" is a variable defined with that name!!!
I think that doesn't matter if a variable name contains an english error!!!

I repeat my questions:
1) why the program terminate when the strem input reachs the EOF?
2) Is there a way to avoid this??? Is the a way to do this with a batch file???

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: redirection input

#11 Post by foxidrive » 17 Feb 2012 08:20

fdaniii wrote:My question is: why if i call the dos program in this way : C:\>myprogram.exe<myinput.txt
(with the redirection input file)
the program terminates the execution also if in the myinput.txt file the "q" character is not present?

I have the necessity to change the file input.text from MATLAB and repeat the do-while when needed.
How to implement this? It is possible to keep the myprogram in wait of parameters from file? Or this is not feasible, beacose the program will terminate the execution when EOF is reached?


Assuming your program is correct - your issue seems to be that you are providing an input file via redirection and it terminates when the input file is read.

Why not write an exe that reads one character and loops, without any tests to end the program. You will find out for yourself what happens if the input file is not completely read.

fdaniii
Posts: 7
Joined: 16 Feb 2012 09:07

Re: redirection input

#12 Post by fdaniii » 17 Feb 2012 09:20

Code: Select all

Why not write an exe that reads one character and loops, without any tests to end the program. 

i need to restart the loop only when needed!! i cannot start the loop in continuous way....

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: redirection input

#13 Post by foxidrive » 17 Feb 2012 09:24

What is the interface to the controller? A serial port?

fdaniii
Posts: 7
Joined: 16 Feb 2012 09:07

Re: redirection input

#14 Post by fdaniii » 17 Feb 2012 09:43

Code: Select all

What is the interface to the controller? A serial port?

no, i communicate with the microcontroller with an ethernet interface...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: redirection input

#15 Post by foxidrive » 17 Feb 2012 10:03

fdaniii wrote:The program has the following structure (in pseudocode):

initialize some parameters;
do {
execute some operations;
while(!input parameter is "q")

My question is: why if i call the dos program in this way : C:\>myprogram.exe<myinput.txt
(with the redirection input file)
the program terminates the execution also if in the myinput.txt file the "q" character is not present?


So in the do-while loop you are reading input. Do you run out of input by reading all that is in the original file?
I would guess that redirection allocates a buffer and reads the input file until it gets to the end of data.

As a test, remove the line that gets input and let it churn through some steps, merely printing to the console. See what happens.
It should continue to print to the console and not terminate.

Let us know what happens and we can try and figure out the next test.

Post Reply