-2

Say I have the following simple C++ program,

#include <bits/stdc++.h>
using namespace std;

int main() { //no argv or argc allowed
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        // do whatever
    }
}

and the following command in terminal:

g++ -std=c++17 -O2 -lm b.cpp && ./a.out

When I run this command, it will ask for my input and I have to copy and paste over the input like the following:

7
1 2 
2 4 
2 6
7 5
3 6
4 6
7 2

Is there a way for this to happen in one pass? I don't mind copy and pasting each time, however I am planning on making a shell script to automate this for me. Basically, it takes the input stored in a remote HTTP server, and then passes it through a C++ program. The caveat is, for some reason I am not allowed to write files, so I can't do fstream/freopen.

VJZ
  • 19
  • 1
  • 9
  • 1
    Just put the data into a file and redirect it to the program. `./a.out < my_data_file` – kaylum Jun 07 '21 at 22:41
  • *The caveat is, for some reason I am not allowed to write files* – user4581301 Jun 07 '21 at 22:41
  • Side note: `freopen` isn't something you want to do most of the time anyway. I see it all the time in competition code and I've never managed to wrap my head around why they do it. Just open a smurfing file stream normally. – user4581301 Jun 07 '21 at 22:43
  • @kaylum Yes user4581301 is correct I cannot write to files because this isn't my machine, I only have access to the shell script and the rest is unaccessable, so is it possible to execute the command within the shell script itself? – VJZ Jun 07 '21 at 22:45
  • Best I can do is have `./a.out < echo 'input goes here'` in the script. Unfortunately my bash is too weak to know if that works with multi-line input. Fortunately it doesn't look like you care if it's multiline or not. – user4581301 Jun 07 '21 at 22:49
  • @user4581301 Can I do `./a.out < echo '1\n2\n3\n4\n5'`? – VJZ Jun 07 '21 at 22:56
  • Sorry, I misunderstood the question initially. Please see answer below for one possible solution. – kaylum Jun 07 '21 at 22:58
  • Unrelated: Please [don't use `#include bits/stdc++.h`](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.). It will only get you into trouble. Instead, include just the headers you need. [cppreference](https://en.cppreference.com/w/) has a list. – Paul Sanders Jun 07 '21 at 23:28

2 Answers2

2

Process substitution is not the right way to do this. Use a heredoc:

g++ -std=c++17 -O2 -lm b.cpp && ./a.out << EOF
7
1 2 
2 4 
2 6
7 5
3 6
4 6
7 2
EOF
William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

One way is to use bash process substitution to create a "file" from a command. And then redirect that file to the program.

Example:

#!/bin/bash

data="
7
1 2 
2 4 
2 6
7 5
3 6
4 6
7 2
"

./a.out < <(echo ${data})

Note that the data string is not really multiline in this case but that is not required for the program you have shown.

kaylum
  • 13,833
  • 2
  • 22
  • 31
  • 1
    `echo ${data}` will discard the newlines. If the newlines are to be retained, use `echo "${data}"` The way the user's program is reading input, the newlines are not important, but quoting variables in this situation is advised. – William Pursell Jun 07 '21 at 23:19