1

I need to know how do I work in C++ with the Unix parameter < and > someone told me if I run the program with ./program < input file > output file the program will read from the file with is declared behind < and it will write out to the file with is declared after the >

actual the code looks like this

int main(int argc, char** argv) {

FILE* filein = fopen(argv[1], "r");
FILE* fileout = fopen(argv[2], "w");

...

so I want to put the files wich I declare at the program start to handle here because I am going to use filein and fileoutlater in the program.

I'm only allowed to work with the stdio.h so please keep it basic.

Thanks in Advance

2
  • You don't need the fopen. If you use cin you will read the file that was piped to your program. Likewise, the output from cout will be piped to the output file presented to your program. Commented Oct 25, 2014 at 15:48
  • Can you give me a Code example? Actually the code is running with declared files, and I need a pointer to the file because I'am going to check the file line by line and I also do End-of-File check. I don't know how to use cin and cout. FILE* filein = cin; won't work. Commented Oct 25, 2014 at 15:53

5 Answers 5

5

This shell cmd syntax

./program < input_file > output_file

doesn't have to do anything with the parameters passed to main

int main(int argc, char** argv) {

You can just refer to std::cin and std::cout for the mentioned input and output files.

It's a shell feature and called standard input/output stream redirection. If you want to pass extra parameters to your program via argv you'll usually use the following syntax

./program -x --opt1 < input_file > output_file

As you state

"I'm only allowed to work with the stdio.h so please keep it basic."

In this case you can use the predefined stdin/stdout macros.

FILE* filein = stdin;
FILE* fileout = stdout;

This is what you should use as default. If you want to have the additional feature, that the user specifies particular input/output file names as program arguments, you should also check the command line parameters passed to your main:

int main(int argc, char* argv[]) {
    FILE* filein = stdin;
    FILE* fileout = stdout;

    if(argc > 1) {
        filein = fopen(argv[1], "r");
    }
    if(argc > 2) {
        fileout = fopen(argv[2], "w");
    }
}

By default your program reads from standard input and writes to standard output, if done as above. So just calling the program without any parameters, leaves a prompt to the user to input something.

This is the preferred, and most flexible style for implementing console programs, that should process streamed input and transform (process) to any output.

Sign up to request clarification or add additional context in comments.

3 Comments

OK. Thanks so far, but how do I use std::cin and std::cout to set it as my FILE* filein and FILE* fileout
@Criska I've just updated my answer to catch up with this requirement.
@Criska Usage of std::cin and std::cout, std::istream, std::ostream respectively, is even easier (syntactically) in my opinion BTW.
4
void main()
{
  printf("Hello World");
}

this is a simple example of '>' cmd

if you complie the above code and then run the cmd

./program 

in your console will output Hello World

if you run

./program >1.txt

then you will see no output in the console but you will find a file named 1.txt in the folder (just us ls cmd to see the files) and int 1.txt (use vim 1.txt to see the content) you will see the output Hello World

and in this case > cmd is used to redirect the output from the console to the 1.txt file

2 Comments

Yes this make Sense. But what if I need to check if the file input is at it's end, is there a possibility like EOF?
if u use '<' cmd then u do not need to think anything about 'input file' just lik the '>' cmd showed about, nothing about file opreation is used in the code. the OS help you handle those stuff
1

Here is a working example:

#include <stdio>
int main(void)
{
  // Read in the data from the piped file
  std::string  text;
  std::cin >> text;

  // Send output to the redirected output file
  std::cout << text;

  return 0;
}

Again, there is no need to use fopen or any variable of type FILE because the operating system has redirected the cin stream to use the file piped into the program.

Edit 1: Using C-style I/O
You can also use the C-stlye I/O using the standard input and standard output.

#include <cstdio>
int main(void)
{
  int a;
  scanf("%d", &a);
  printf("%d\n", a);
  return 0;
}

Whether you use the C-style I/O or the C++ I/O, the operating system has redirected the standard input and output streams to read and write from the files you passed (via redirection) to your program.

Comments

0

The < and > are redirectors, you just need to read from stdin and write to stdout. Your program will read the contents of "input file" via stdin and everything your program is writing to stdout will be in output file.

You don't need to handle those files yourself.

Comments

0

When running a program from a UNIX shell, using input and output redirection will cause the shell to redirect the standard output and input streams to those files. Once that happens, your program need not and will not know about it. Simply use printf and scanf (etc.) as you normally would.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.