Pipe an input to C++ cin from Bash - c++

I'm trying to write a simple Bash script to compile my C++ code, in this case it's a very simple program that just reads input into a vector and then prints the content of the vector.
C++ code:
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
string s;
while (cin >> s)
v.push_back(s);
for (int i = 0; i != v.size(); ++i)
cout << v[i] << endl;
}
Bash script run.sh:
#! /bin/bash
g++ main.cpp > output.txt
So that compiles my C++ code and creates a.out and output.txt (which is empty because there is no input). I tried a few variations using "input.txt <" with no luck. I'm not sure how to pipe my input file (just short list of a few random words) to cin of my c++ program.

You have to first compile the program to create an executable. Then, you run the executable. Unlike a scripting language's interpreter, g++ does not interpret the source file, but compiles the source to create binary images.
#! /bin/bash
g++ main.cpp
./a.out < "input.txt" > "output.txt"

g++ main.cpp compiles it, the compiled program is then called 'a.out' (g++'s default output name). But why are you getting the output of the compiler?
I think what you want to do is something like this:
#! /bin/bash
# Compile to a.out
g++ main.cpp -o a.out
# Then run the program with input.txt redirected
# to stdin and the stdout redirected to output.txt
./a.out < input.txt > output.txt
Also as Lee Avital suggested to properly pipe an input from the file:
cat input.txt | ./a.out > output.txt
The first just redirects, not technically piping. You may like to read David Oneill's explanation here: https://askubuntu.com/questions/172982/what-is-the-difference-between-redirection-and-pipe

Related

Reading input using command in c++

I am trying to read a string into my program from the terminal. So the command I want to use is eg g++ -g -std=c++11 main.cpp -o out to compile, then ./out < file.txt to run my program. But I, however, get an error when I use a < symbol when running my program.
int main(int argc, char** argv){
cout << "Checking this " << argv[1] << endl;
return 0;
}
I want my program to output Checking this file.txt but I want to run it this way, ./out < file.txt NOT THIS AWAY ./out file.txt
This is not possible with this configuration. You would have to run the program as ./out file.txt and then have the program read in from file.txt which will have the file name stored in argv[1] assuming this order.
POSIX compliant shells when you run ./out < file.txt will just give your program the contents of file.txt in stdin. The < file.txt part is not visible to your program.

Why does g++ compiled file end with a % sign?

#include <iostream>
int main()
{
std::cout << "Hello";
}
I compiled it using g++ Hello.cpp
I received the following output when I ran the compiled a.out file using ./a.out
Why do I keep getting a % sign at the end of the output?
./a.out
Hello%
The % you see there might actually be your shell prompt, and not part of your program output. You're not writing a new line after your output, so the shell prompt appears at the very end of the output of the last command.
Possible solutions:
Append a newline to the end of the output with + "\n".
Add a std::endl to the end of your output.

passing input value from bash script to a c++ executable

I have a c++ code which requires an input value. I would like to have a bash script to run my c++ executable file automatically. My bash script is below:
#!/bin/bash
g++ freshness.cpp -g -o prob
for((i=0;i<30;i++))
{
./prob<$2 ../../data/new_r.txt ../../data/new_p.txt ../../data /t_$1.txt >> result.txt
}
./cal result.txt
rm result.txt
My main.cpp is below:
int main(int argc,char*argv[])
{
map<int, struct Router> nodes;
cout<<"creating routers..."<<endl;
Create_router(argv[1],nodes);
cout<<"creating topology..."<<endl;
LoadRouting(argv[2],nodes);
cout<<"simulating..."<<endl;
Simulate(argv[3],nodes);
return 0;
}
There is a cin in Create_router(argv[1],nodes), like cin>>r_size;
Many thanks in advance.
./prob < $2
means to redirect the input of the program to a file whose name is in the $2 variable.
If $2 is the actual input data, not a filename, then you should use a here-string:
./prob <<< "$2" ../../data/new_r.txt ../../data/new_p.txt ../../data /t_$1.txt >> result.txt
or a here-doc:
./prob <EOF ../../data/new_r.txt ../../data/new_p.txt ../../data /t_$1.txt >> result.txt
$2
EOF
or pipe the input to the program:
printf "%s\n" "$2" | ./prob ../../data/new_r.txt ../../data/new_p.txt ../../data /t_$1.txt >> result.txt
The here-string method is the simplest, but it's a bash extension. The other methods are portable to all POSIX shells.

Redirecting I/O only with <iostream> [Windows]

[#tl;dr] I have Visual Studio Ultimate 2013 and Eclipse Neon v2(C++),
and I need to redirect the output of my program using DOS format, but
I have no idea how.
Im on Windows btw.
OK.. so I have this class assignment where I have to write a program like this:
#include <iostream>
#include <string>
using namespace std;
/* Check if the Character is lower case or not */
bool checkLowerCase(char c) {
if (c >= 'a' && c <= 'z') return true;
return false;
}
/*
* If there is any lower case letter, it will replace it will a upper case.
* example:
* "Tauros" will become "TAUROS"
* "auHU" will become "AUHU"
*/
string fixer(string s)
{
char right = ('a' - 'A');
string a = s;
for (unsigned int i = 0; i < a.length(); i++)
{
if (checkLowerCase(a[i])) {
a[i] = a[i] - right;
}
}
return a;
}
int main(void)
{
while (1) //infinite loop
{
string line;
getline(cin, line);
if (!cin) { //Professor wants us to only check end of input like this
return 0;
}
line = fixer(line);
cout << line << endl;
}
}
The input was:
aaaaaa
bbbbbb
cccccc
The output was:
aaaaaa
AAAAAA
bbbbbb
BBBBBB
cccccc
CCCCCC
Thanks for reading this far. Ok, so here's my problem.
The output is all messed up, so I need to redirect the output somewhere else (at least for testing).
I know how to do that using , , holding each line in a Array of String, reallocating if needed and then print what is on the array, but, unfortunately, my lecturer demanded us only to include and
Ohh, I dont know if it will matter, but we may not use char*, only the class string.
My lecturer told us that we have to use DOS format. But I have no clue how to do that. If someone can tell me how to do either redirect the input or the output is finee...
I have in my PC both Eclipse C++ (working glitchy) and Visual Studio Ultimate 2013 (working fine).
[Edit] Im on Windows.
AGAIN: I may only include and
For more information, here's his slide on DOS format.
*For testing purposes one redirect to/from a file
*DOS formatting will have unexpected consequences
– The end-of-line is the CR-NL combination
– A line read from the file will end with CR
– The CR character is the command to erase the
previous line!
./main < infile.txt Input is from infile.txt
./main > outfile.txt Output is to outfile.txt
./main < infile.txt > outfile.txt Both input and output are redirected
OK, so... I found instructions that helped me here:
https://msdn.microsoft.com/en-us/library/ms235639.aspx
I opened my project folder in the VisualStudio, created 2 files.
input.txt
output.txt
I opened the Developer Command Prompt for VS2013 that I had in my PC.
cd ["C://my_project_path"] //go to my project folder
cl /EHsc file.cpp
/*
cl /EHsc will compile my program (I think it only compiles one file at a time.
I have yet to test it).
*/
file < input.txt > output.txt // this command will run my program
So when I run my program like this... Instead of waiting for an input from keyboard, my program will read input.txt as the standard input. And when I try to print something to the output, it will write on the output.txt instead.
[EDIT] at the end of the link I pasted, there are explanations on how to compile multiple files and what exacly does the /EHsc do exacly

How to use a wildcard expression within system()

The following command runs fine on my embedded Linux (Beaglebone Black):
echo bone_pwm_P9_21 > /sys/devices/bone_capemgr.?/slots
But not when using this small C++ program:
#include <stdlib.h>
#include <string>
int main {
system(std::string("echo bone_pwm_P9_21 > /sys/devices/bone_capemgr.?/slots").c_str());
return 0;
}
The problem involves the '?' question mark, that is used as a wildcard.
When the question mark, in the std::string that is passed to system(), is replaced with a normal character, the system() function evaluates the command perfect.
Solutions I've tried without success:
replace ? with \?
replace ? with *
Apart from your code not being compilable, this fails because system(3) runs sh, often a minimal shell provided by dash or busybox.
Meanwhile, your interactive login uses bash, ksh or some other more comfy shell.
dash and busybox sh do not do glob expansion on redirections, while bash and ksh do. Here's a demonstration of the behavior you want courtesy of bash:
$ touch file.txt
$ bash -c 'echo "bash contents" > *.txt'
$ cat file.txt
bash contents
Meanwhile, the problem you're having with e.g. dash:
$ dash -c 'echo "and now dash" > *.txt'
$ ls
*.txt file.txt
$ cat '*.txt' # Instead of expanding, the path was taken literally
and now dash
$ cat file.txt
bash contents
To fix this, you can (in order of preference)
Write your C program in C code instead of shell script
Call a better shell with execve.
Rewrite to not write to a glob, e.g. echo "stuff" | tee *.txt > /dev/null
Call a better shell with system, e.g. bash -c "echo stuff > *.txt"
NOTE: As πάντα ῥεῖ pointed out the system() command calls the shell which will usually do the expansion when presented with the correct wildcard: *. This answer is thereful more appropriate if you want the control to make each system() call separately or if the underlying shell is limited.
Original answer:
Perhaps you could use wordexp for this to construct your strings before you make the system() call:
#include <string>
#include <vector>
#include <iostream>
#include <wordexp.h>
std::vector<std::string> expand_env(const std::string& var, int flags = 0)
{
std::vector<std::string> vars;
wordexp_t p;
if(!wordexp(var.c_str(), &p, flags))
{
if(p.we_wordc)
for(char** exp = p.we_wordv; *exp; ++exp)
vars.push_back(exp[0]);
wordfree(&p);
}
return vars;
}
int main()
{
for(auto&& s: expand_env("$HOME/*")) // <= Note the wildcard '*'
std::cout << s << '\n';
}
In your specific case you could perhaps use something like this:
int main()
{
std::vector<std::string> devices = expand_env("/sys/devices/bone_capemgr.*/slots");
for(std::vector<std::string>::size_type i = 0; i < devices.size(); ++i)
system(("echo bone_pwm_P9_21 > " + devices[i]).c_str());
}