inputing a file with unix command

So say I have a program called detect_cycle.cpp

Then I compile it and name it detect_cycle

Then from my unix terminal I type:

detect_cycle < data_file

where data_file is in the same directory as my detect_cycle executable.

What would I write in main so that I would know what file the user is specifying. For example they could have written

detect_cycle < data_file_2

Then I would need main to know that it is data_file_2
There's no need for the < operator. You can just directly input the file location into your program as an argument.

int main(int argc, char * argv[])

Be careful when doing comparisons.

-Albatross
Last edited on
The < operator is the unix command for take the file on the right and put it in the program on the left.

This is homework so the instructor specifically asked that he be able to execute the program from the unix terminal with that specific call. The problem is argv will not include data_file in its array. For instance

detect_cycle data_file

argv = {detect_cycle, data_file}

detect_cycle < data_file

argv = {detect_cycle}
Ah. So. This is homework, and it's required that... wait a minute. Which shell are you using? Bash? Tcsh? Zsh?

-Albatross
Just read from stdin. For example, try this:
1
2
3
4
string line;
while( getline( cin, line ) ) {
    std::cout << line << std::endl;
}
Apparently this is more of a Unix question then a c++ question.

Say you have the program:

1
2
3
4
5
6
7
8
//Hello_World.cpp
#include <iostream.h>
using namespace std;

int main ()
{
    cout << "Hello World";
}


Furthermore you have no other files in the directory except it's compiled version:
Hello_World

If you type:

Hello_World > Stuff

from the terminal you will create a file called Stuff. Inside that file will be


Hello World


Your terminal will look like this:


/home/
% Hello_World > Stuff
/home/
% 


Notice that Hello World got print to the file Stuff not to the terminal. That is because the '>' changes standard output to the thing on the right. If you didn't include '>' on the command line and typed:

Hello_World

from the terminal then Stuff would not be created and your terminal would look like this:



/home/
% Hello_World
Hello World
/home/
% 

@moorecm:

He needs to use the < operator in the shell which begs the question what shell he's using.

EDIT: Tcsh? Should have known...

-Albatross
Last edited on
Thanks for the help! I didn't know that '<' changed the standard stream when i first made this post. cin and cout are obviously in the std namespace so changing location of standard stream changes where cin and cout read/write. (which is what moorecm was getting at)

I finally found something useful on wikipedia

http://en.wikipedia.org/wiki/Redirection_(computing)

that made me realize what I was not understanding. In case you're still curious (Albatross) I'm using the solaris operating system and they have a unix terminal and it is tcsh.
Last edited on
He needs to use the < operator in the shell which begs the question what shell he's using.


His code will be independent of the shell. Redirection is redirection... It's equivalent to cat input_file | detect_cycle. The input is redirected to stdin. In fact, unless I'm mistaken, bash, sh, csh, tcsh, and zsh, all support it.
@moorecm

Okay... I need to brush up on my Unix shells (I've been using Ruby as a shell for some time now).

-Albatross
Topic archived. No new replies allowed.