Reading input using command in c++ - 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.

Related

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.

Change the code so that the name of the file you will write, will be entered from command line

I've changed the code. And I do the following:
Open Terminal
Write gcc program.c -o program1
./program1
I get Error!
I conclude that no file is being created, the pointer fptr==0 and that is why I get that error. Also when I enter more strings or integers after ./program1 (like ./program 1 2) shows "Error!" again. Here's the code:
#include <stdio.h>
int main(int argc, char *argv[]){
int n;
FILE *fptr;
fptr=fopen(argv[0],"w");
if(fptr==NULL){
printf("Error!");
exit(1);
}
printf("Enter n: ");
scanf("%d",&n);
fprintf(fptr,"%d",n);
fclose(fptr);
return 0;
}
argv[0] is the name of the program, if you pass the file path, you should use argv[1]
Try to launch like:
$ ./program mypath/file.name
argv[0] is program, argv[1] is mypatch/file.name
This instruction tries to open the executable of your programme in write mode:
fptr=fopen(argv[0],"w"); // 0 is the programme name
This fails certainly because of administrative priviledges that are (fortunately) missing.
You may add a more usefull error message using perror()
If you want to write data in a file provided on the command line, you should consider:
if (argc<=1) {
printf ("Missing command line argument !\n");
exit(1);
}
fptr=fopen(argv[1],"w"); // first argument is in 1
...
and calling your programme from the command line:
./program1 myfile.txt

Piping input to an already running cpp program?

include
Lets assume that this is the code I am running:
int main(int argc, char **argv) {
bool running = true;
string lineInput;
while (running)
{
while (cin >> lineInput)
{
cout << lineInput;
}
}
return 0;
}
What I would like to have happen is that I can call start a program from terminal by typing "./myProgram" That part is fairly straight forward. The part I'm not sure how to do is make it so that I can at a later point in time type echo "some text to echo" | myProgram and be able to have my program then print that text back out to the terminal.
Right now I can only make it work if I type:
echo "blah blah blah" | ./myProgram
So my goal is to have two separate steps. One where I start my program, and a second when I pipe it some input to use
I'm thinking you could do this with a named pipe.
mkfifo mypipe
./myProgram < mypipe &
cat file1.txt file2.txt file3.txt > mypipe
You can use mkfifo, and just read from that in the program as from an ordinary file.
There's also
pipe or socket_pair (bi-directional)

Pipe an input to C++ cin from Bash

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