954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Running a CPP code from a Shell

Hi all,

I want to run a cpp code a couple of times using a shell. Cpp code needs a text file as an input. I have been trying to pass the file to the cpp code no success.
The following is what I have done so far:

#! /bin/bash

g++ 3Distance.cpp
./a.out 
echo file1.xml 
> 3d-1.xml

For later I like to use file$i.xml instead of file1.xml in the above.

any suggestion is very much appreciated.

SakuraPink
Light Poster
31 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Depends on how the executable (which you refer to as cpp) expects the input:
1. It reads the filename:

echo file1.xml | ./a.out > 3d-1.xml

2. It reads the contents of a file:

./a.out < file1.xml > 3d-1.xml

3. It takes a filename as an argument:

./a.out file1.xml > 3d-1.xml

All of the above presumes that a.out writes its output to stdout.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

Thanks. I used the first one and the code run as I expected but using the 2nd one it seems that the cpp code run but it did not use the file.xml data. I am guessing that is what you mean by saying it depends how my cpp code takes the input.

SakuraPink
Light Poster
31 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You