I am using QProcess to communicate with console application: i am writing some words and read outputs. But i want to write line via QProcess. For example, i have the next console app:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
string action;
do
{
cout << "(test)";
cin >> action;
cout << action;
if(action.length() > 10)
{
cout << "\t very long string";
}
cout << endl;
}
while(action != "exit");
return 0;
}
So, i can't pass parameters via QProcess::exec or QProcess::start, because it pass parameters to char* argv[]. I should pass it after launching QProcess. I've tried use QProcess::write, but there are problem: if i use
process.write("oneWord\n");
i'll get success. But if i use
process.write("several words\n");
my program will write all this word separately and it looks like
process.write("several\n");
process.write("words\n");
Console application doesn't recognize it as one string. I've tried using different ways: write line in double brackets,
process.write("\"several words"\\n");
and
process.write("\"several words\n""\);
use protected methods QIODEvice::setOpenMode and set QIODevice::Text flag, use QDataStream, use special symbols like \r, \n, \t and different combination. Also, i tried to use multiple QProcess::write
process.write("several");
process.write("words\n");
I know, that QProcess inherits QIODevice and there are opportunity to deal with it like a file (input & output). But it doesn't matter if words will be written to file separately in file. In my case, it does matter.
Can anyone help me?
Related
I have a txt file called test.txt that looks like this,
hi this is a test
and I have a c++ file called selection_sort.cpp that looks like this,
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
cout << argc << endl;
for (int i = 0; i < argc; i++) {
cout << argv[i] << endl;
}
return 0;
}
right now when I compile my program in my terminal with
g++ selection_sort.cpp -o selection_sort
and then try and print out all of of the arguments I am trying to pass using my code like this
./selection_sort < test.txt
but it only outputs
./selecton_sort
I would like it to output
./selection_sort
hi
this
is
a
test
What am I missing or doing wrong? I need to use the <.
What am I missing or doing wrong? I need to use the <.
This is a shell operator that sends the content of the file to the standard input of the application.
./app < text.file
Will read the file text.file and send the conent to the standard input of the application app. In C++ you can read the standard input via std::cin.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
std::string word;
while (std::cin >> word) {
std::cout << word << "\n";
}
}
< in bash is used to redirect input. In other words, it redirects the standard input for your process to the file. However, command line arguments (e.g. argv arguments) are not read through standard input, so you cannot capture them from a file by redirecting input.
Rather, they are provided as arguments when running the command in bash to begin with. You can accomplish your goal like so:
./selection_sort $(cat test.txt)
cat is for concatenating files, but if you supply it just one file, it will just output the contents of the file through standard out. The $(x) operation will execute the x command in a subshell, capture its standard output (which in this case is the contents of the file), and then do variable substitution to replace $(x) with said contents.
Edit:
Or, you can just change the way the arguments are accepted, so that they are accepted via standard input. It depends on how you want to be able to run the program.
I've seen a couple of solutions to this question, but I'm still running into an issue. I've tried several of the solutions I've seen on this site, but I keep having the same issue. I'm not sure how to make this work, and I'm also not sure why it's not working, since I'm really new to C++.
A brief explanation of what I am attempting to do: I'm writing a simple old-school text-based story/game for a video game design club at my school, and at several points I have the user make decisions, or input things such as their name, which is then used in an output line, as it would be referenced several times. I have had no problem doing that, using something simple like this:
#include <iostream>
#include <string>
#include <limits>
#include <windows.h>
using namespace std;
int main(){
string name, place ;
cout << "???: What is your name? ";
getline (cin, name);
cout << "???: Hello, " << name << "!\n" << "\n";
}
My problem is that I'd like to have the text appear one character at a time, like dialogue, but whenever I try to use something I've seen written by others, it doesn't seem to like it very much. The only one that I tried that I can now find again is this:
#include <iostream>
#include <unistd.h> // include <windows.h> on windows
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
usleep(60000); // use Sleep on windows
}
}
int main()
{
type_text("Hej hej hallÄ!");
}
Apparently there is some sort of conflict regarding my attempt to output the name back to the user when I try to use that code with what I've written. I'm not really sure what the problem is, since I'm so new to C++, can anyone help me out?
Consider using std::this_thread::sleep_for, as it is standard C++11. Example:
#include <iostream>
#include <thread> // standard C++11
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
}
int main()
{
type_text("Hello, World!");
}
If you have access to a C++14 compiler, you can simply make use of the std::chrono user-defined literals and have a more "natural" syntax:
using namespace std::literals;
std::this_thread::sleep_for(60ms);
My code is intended to tell the user whether the string entered is a keyword in c++.
I am reading the keywords from a file into a set and then checking if the user supplied string is in it.
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <fstream>
using namespace std;
int main()
{
set<string> key;
fstream fs;
string b;
fs.open("keywords.txt",fstream::in);
while(getline(fs,b))
key.insert(b);
b.clear();
for(auto x:key)
cout << x << endl;
cout << "Enter String user\nPress exit to terminate\n";
while(getline(cin,b))
{
if(b == "exit")
break;
if(key.find(b) != key.end())
cout << "This is a keyword\n";
else
cout << "This is a not a keyword\n";
b.clear();
}
fs.close();
}
The keywords.txt file is just a list of keywords and can be obtained from here
The problem is that my program reads all keywords correctly but for some of them such as false,public it cannot find them in the set.
i.e. when I enter false as user input
it says, "This is not a keyword."
Considering your input file, I think you have some keyword names with trailing spaces.
"catch "
"false "
You can trim the strings before inserting in the set to remove spaces, using boost::trim or your own trim (see this question for instance.)
(If you want some advice as for your code:
You can use std::ifstream like this for input file streams:
std::ifstream file( "keywords.txt" );
You do not need to call .close() at then of the scope, it will be done automatically thanks to RAII.
You should not reuse the same std::string objects for every purpose, you can declare new string objects close to their use. You should give them better names like "line" instead of "b". Doing this, you don't need to call ".clear()" for your strings.
Every line has just one word, you could use while(fs>>b) the >> will ignore the spaces (from moldbinlo & wangxf comments)
)
I am c++ beginner and this is for school..
I am trying to read a file about 28kb big. The program works but it doesnt print the first 41 lines. It works fine with a smaller file.
At first i was reading into a char array and switch it to strings.
i also tried changing the log buffer but it apparently it should be big enough..
I feel like this should be very simple, but just cant figure it out..
Any help will be greatly apreciated..
Thanks!
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <cerrno>
using namespace std;
struct espion
{
char nom[30];
char pays[20];
char emploi[29];
};
int main()
{
const int MAX_NOM = 30, MAX_PAYS = 20, MAX_EMPLOI = 29;
char nomFichier[50] = "espion.txt";
ifstream aLire;
aLire.open(nomFichier, ios::in|ios::binary);
if(!aLire.is_open()){
exit(EXIT_FAILURE);
}
std::string infoEspion;
while(aLire)
{
infoEspion.clear();
std::getline(aLire, infoEspion);
cout << infoEspion ;
}
aLire.close();
system("pause");
return 0;
}
From the system("pause"), it looks like you're running on Windows. With ios::binary, the end-of-line marker is not translated, and the cout << infoEspion; statement prints these "raw" lines in such a way that all of the lines are written on top of each other. (More specifically, each line will end with a return but no newline, so the cursor goes back to the start of the same line after executing each cout statement.) If you take out the ios::binary, you will echo all of the input on a single, very long line. Changing the statement to cout << infoEspion << endl; will echo all of the lines.
Trying to get some basic understanding of console functionalities. I am having issues so consider the following...
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
/*
This is a template Project
*/
void MultiplicationTable(int x);
int main()
{
int value = 0;
printf("Please enter any number \n\n");
getline(cin, value);
MultiplicationTable(value);
getchar();
return 0;
}
I actually based this off code from http://www.cplusplus.com/doc/tutorial/basic_io/ . My IDE is not recognizing getline() so of course when I compile the application. I get an error
'getline': identifier not found
Now take a look at this code
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
/*
This is a template Project
*/
void MultiplicationTable(int x);
int main()
{
int value = 0;
printf("Please enter any number \n\n");
cin>>value;
MultiplicationTable(value);
getchar();
return 0;
}
When I execute this line of code the console window opens and immediately closes. I think I a missing something about cin. I do know that it delimits spaces but I don't know what else. what should I use for input to make my life easier.
The function getline() is declared in the string header. So, you have to add #include <string>.
It is defined as istream& getline ( istream& is, string& str );, but you call it with an int instead of a string object.
About your second question:
When I execute this line of code the console window opens and immediately closes
There is probably still a '\n' character from your input in the stream, when your program reaches the function getchar() (which I assume you put there so your window doesn't close). You have to flush your stream. An easy fix is, instead of getchar(), add the line
int c;
while((c = getchar()) != '\n'){}
This will flush your stream until the next line-break.
Remark: conio.h is not part of the c++ standard and obsolete.
The getline function reads strings, not integers:
#include <string>
#include <iostream>
using namespace std;
int main() {
string line;
getline( cin, line );
cout << "You entered: " << line << endl;
}
You are exiting the program before you can view the results because (I'm guessing) you double-clicked the .exe file from inside a Windows Explorer (or the Desktop) view in order to execute. Instead, go to Start, Run, type in cmd.exe and open a command window. Navigate to where your program resides. Type in your program's name on the command line and execute. It will stay open until you intentionally close the command window.