I am trying to incorporate the use of a batch file towards my program. For this I just want that after the program has starting running to send the file content's to stdin, in which case the program should do the rest? Is this possible, how so? Or should I be doing something completely different. Thanks.

Recommended Answers

All 19 Replies

I think you might be confused about the purpose of a batch file. Batch files are interpreted by the operating system -- not a program that you write. Yes, you can write a program that reads a batch file, but what will it do with it???

Hmm let me rephrase then.. I'll just call it an input file then, I think what I'm trying to do is similar in a way though. The file's contents contains series of arguments to be interpreted by the program through the use of standard input.

I guess a simple example would be if the file read:

Which could normally be read from the program through stdin:
Enter expression: smile
Enter expression: frown
Enter expression: speechless
Enter expression: exit

:-) :-( :-X

How would I go about writing this file to stdin and is it advisable to do so?

Step 1: create the text file that contains the commands

Step 2 write the c++ program using std::cin as you normally would. Here's an example

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
    string line;
    while( cin >> line )    
        cout << line << "\n";
    return 0;

Step 3: on command prompt retired the text file into your program

c:>myfile <try.txt <Enter>


C:\dvlp\test3\Debug>test3 <try.txt


ah yes I see thanks!! Adding further: Is there a way to do this within the program? Such as if the file was(possibly) included as a main argument to then pipe it to stdin(C only)?
> myfile try.txt
and having the same result?

If the file was included just open the file. If the file was not included, open stdin instead.

if (filename_entered)
    open file
    open stdin
end if

hmm thanks for the reply but I'm confused. Isn't stdin already open so no need for else? And wouldn't that force you to do one or the other? Like in the example where a file is loaded initially but then the user could continue to input after that.

He means if argc > 1 then you can assume argv[1] is the filename. But if argc == 0 or 1 then you can assume the filename was not entered on the command line like your example

int main(int argc, char* argv[])
    FILE* file = 0;
    if( argc == 2)
        file = fopen(argv[1], "r");
        file = stdin;
    // now use file however you want to

    // when ready to close the file
    if( argc == 2)

Yeah, that's what I meant... Esp since it was pseudo code, not C code.

hmm I am still trying to understand the reasoning behind this, the implementation part you show I understand. When I add a file as a main argument, I would have to handle that file first and then prompt from stdin right? In the basic example I show below, the prompt for a new value occurs in a loop and exits it when say the user types 'exit'. If a file was included in main argument using this wouldn't I have to use two loops? One to loop through the file's contents, then one for possible user input afterwards? Or is there a way to put the file to stdin in which case it would be handled in one loop?

I thought you wanted to read from a file or stdin. If this isn't true you might want to give us all the information we need to understand what you're trying to do.

Sorry if I didn't explain it well enough, which looks like the case. That part you say is true initially, but after a file is input in the main argument to be read I want the program to continue read on stdin if there's no command in the file, let's say 'exit', which causes the program to terminate. That's why I was asking if the file could be transferred to stdin in which case the program will take it through stdin and then continue prompting for new commands naturally, rather than me handling the file(if added) first and then opening the prompt for stdin.

I'm also not clear as to the naming of an input file as a 'batch' file. Is that name specific to input files for OS command line interfaces only?

You open your input file. Read it until the end of the file. Close it. Set file pointer to stdin and keep reading until the user inputs exit.

They are two separate input sources and have to be handled one after the other.

Batch File description here

>>I'm also not clear as to the naming of an input file as a 'batch' file. Is that name specific to input files for OS command line interfaces only?

No. The way you want to use them they are not batch files at all but just normal every-day text files. So you can name it anything you wish. If you want the operating system's command processing handling to process the file then it must have *.bat extension.

thanks for the replies, so you do have to handle one after the other, so there is no way to have same functionality as:

%> program < mytext.txt
while invocating like this:
%> program mytext.txt

So the main difference then between a batch file and an input file is that the commands in a batch file invoke something in the context of the environment, whereas the input file contains commands that are to be handled within my progam? Just trying to understand main difference.

Well if you did

$ program < mytext.txt

Then your program will expect data from stdin .. and you will need to enter all of that data until you are done, and then read from the file. There is no way to automatically switch .. you have to make the switch yourself.

At the risk of sounding repetitive,
A batch file (on DOS) or a shell script (on *nix) has a set of commands written for your command line interpreter (DOS or the particular shell you are writing for).

Your input file can have anything in it. Its upto you to decide how your program reads and interprets its contents.

In your particular case I am not sure why you would need a batch file, it looks like you just need input data for your program ?

hmm.. so making the switch in my program would actually mimic what the shell is doing when I use the '<' operator like that, if I understand what you're saying. Is there any way to get that functionality(from the shell) directly within my program, would it not be recommended for portability reasons?

almost done with the batch file question. If I make a command line interface to a software program where a file contains commands for that program to execute, would you call that file a batch file?

almost done with the batch file question. If I make a command line interface to a software program where a file contains commands for that program to execute, would you call that file a batch file?

For the millionth time: batch files are interpreted by the operating system. If the file is something your program is going to interpret then it isn't a batch file, although it can have *.bat file extension.

Get the terminology straight: Application programs do not process batch files, only the operating system shell does that. Whether its pass as a normal argument to the program or redirected doesn't matter -- its still not called a batch file.

If you are going to write your own shell that will replace the Windows cmd.exe, then yes you can call it a batch file. But I doubt that is what you intend to do because then you can't redirect to stdin nor pass as a parameter -- shells just don't work that way.

Thanks for answering that Ancient Dragon :) .. I was attempting to give a logical answer .. but don't think its getting through :confused:

ah much clearer now thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.