I am getting performance issues when using popen in C++. The output shown on the screen looks REALLY slow compared to when I open the subprocess directly. It looks like if it was refreshing every second and not constantly... I even tried not using endl, and sync_with_stdio(false) but nothing seems to make the output faster.
Here is my code:
int main() {
FILE *in;
char buff[4096];
//cout.sync_with_stdio(false);
if(!(in = popen("python websocket/test.py", "r"))){
return 1;
}
while(fgets(buff, sizeof(buff), in)!=NULL){
cout << "====>>" << buff << endl;
}
pclose(in);
return 0;
}
What could be wrong with my code?
Related
Originally was trying to read data using char* but switched to string cause was getting behavior as if there was a missing null terminator. made the problem minimal below but still getting very weird output
int main(int argc, char * argv[]){
// file one: flightData
std::ifstream inFile1(argv[1]);
if (!inFile1.is_open())
{
std::cout << "Could not open the file 1." << std::endl;
return -1;
}
// file two: flightPlans
std::ifstream inFile2(argv[2]);
if (!inFile2.is_open())
{
std::cout << "Could not open the file 2." << std::endl;
return -1;
}
//File three: output
std::ofstream outputfile(argv[3]);
if (!outputfile.is_open())
{
std::cout << "Could not open the output file" << std::endl;
return -1;
}
std::string buffer;
getline(inFile1, buffer);
std::cout<<buffer<<std::endl;
while (getline(inFile1, buffer)) {
std::cout<<buffer;
std::cout<<"help";
}
// flightPlanner system(inFile1);
// system.printF();
// system.planFlights(inFile2,outputfile);
return 0;
}
output is
4
helpDallas|Austin|50|50help
which i'm pretty sure is incorrect, interestingly when i add endl to cout buffer it gives me output i would expect not really sure whats going on
inFile1
4
Dallas|Austin|50|50
Dallas|Austin|50|50
Dallas|Austin|50|50
Dallas|Austin|50|50
When i run in debugger i get the output i expect:
4
Dallas|Houston|50|50
helpDallas|Houston|50|50
helpDallas|Houston|50|50
helpDallas|Houston|50|50help
any idea what could be going on?
Do you need flushing your stdout?
std::cout << std::flush;
Any chance your shell ate your outputs?
Try pipping the output to "cat -A":
./a.out | cat -A
(Drive by comment - I may not know what I'm talking about ^_^)
I have created a pseudo terminal in C++ using the following code:
int main(int, char const *[])
{
int master, slave;
char name[1024];
char mode[] = "0777"; //I know this isn't good, it is for testing at the moment
int access;
int e = openpty(&master, &slave, &name[0], 0, 0);
if(0 > e) {
std::printf("Error: %s\n", strerror(errno));
return -1;
}
if( 0 != unlockpt(slave) )
{
perror("Slave Error");
}
access = strtol(mode, 0, 8);
if( 0 > chmod(name, access) )
{
perror("Permission Error");
}
//std::cout << "Master: " << master << std::endl;
std::printf("Slave PTY: %s\n", name);
int r;
prompt = "login: ";
while(true)
{
std::cout << prompt << std::flush;
r = read(master, &name[0], sizeof(name)-1);
checkInput(name);
name[r] = '\0';
std::printf("%s", &name[0]);
std::printf("\n");
}
close(slave);
close(master);
return 0;
}
It works pretty well in the sense that from another terminal, I can do:
printf 'username' > /dev/pts/x
and it will appear and be processed as it should.
My question is: when I try to use screen, nothing appears on the screen terminal. Then when I type, it comes through to my slave 1 character at a time.
Does anyone know why this is? Or how I can fix it.
I can provide more detail if required.
Thank you :)
Because you're not flushing the buffer after you use printf.
As pauls answer already suggest you need to flush the buffer.
To do so you can use the tcflush function.
The first argument is the int of the file descriptor and the second can be one of the following:
TCIFLUSH Flushes input data that has been received by the system but
not read by an application.
TCOFLUSH Flushes output data that has been written by an application
but not sent to the terminal.
TCIOFLUSH Flushes both input and output data.
For more information see: https://www.ibm.com/docs/en/zos/2.3.0?topic=functions-tcflush-flush-input-output-terminal
I'm working on what will be a pretty large and complicated system and trying to make sure it's as watertight as possible right from the start. Whilst running some memory checks, I noticed something odd when using stringstreams: they don't always seem to release all the memory when they get deleted/go out of scope.
I've tried searching the internet for answers, but most are old (so possibly out of date) and/or are more concerned with refreshing the contents than releasing the memory, so I've not really been able to tell if it's a known issue or a common mistake I'm making.
I've written a simple test to show what's going on:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
float getMemUsage(int& pid)
{
if (pid < 0)
pid = getpid();
char buf[30];
snprintf(buf, 30, "/proc/%u/statm", (unsigned)pid);
FILE* pf = fopen(buf, "r");
if (pf)
{
unsigned size; // total program size
//unsigned resident;// resident set size
//unsigned share;// shared pages
//unsigned text;// text (code)
//unsigned lib;// library
//unsigned data;// data/stack
//unsigned dt;// dirty pages (unused in Linux 2.6)
fscanf(pf, "%u" /* %u %u %u %u %u"*/, &size/*, &resident, &share, &text, &lib, &data*/);
fclose(pf);
return size/1024.0;
}
else
return -1.0;
}
int main(int argc, char* argv[])
{
if (argc < 2)
cerr << "no file specified\n";
ifstream file;
file.open(argv[1]);
int pid = -1;
const float memUseAtStart = getMemUsage(pid);
{
float memUseBefore = getMemUsage(pid);
stringstream sstream;
float memUseAfter = getMemUsage(pid);
cerr << "\tMemory use change after stringstream declaration: " << memUseAfter - memUseBefore << endl;
memUseBefore = getMemUsage(pid);
filebuf* pbuf = file.rdbuf();
memUseAfter = getMemUsage(pid);
cerr << "\tMemory use change after getting file buffer: " << memUseAfter - memUseBefore << endl;
memUseBefore = getMemUsage(pid);
sstream << pbuf;
memUseAfter = getMemUsage(pid);
cerr << "\tMemory use change after copying file contents: " << memUseAfter - memUseBefore << endl;
memUseBefore = getMemUsage(pid);
sstream.clear();
sstream.str( string() );
memUseAfter = getMemUsage(pid);
cerr << "\tMemory use change after 'clearing': " << memUseAfter - memUseBefore << endl;
}
cerr << "Overall memory use change: " << getMemUsage(pid) - memUseAtStart << endl;
file.close();
return 0;
}
Which gives me the following output when called with a file larger than around 32K:
Memory use change after stringstream declaration: 0
Memory use change after getting file buffer: 0
Memory use change after copying file contents: 0.0322266
Memory use change after 'clearing': 0
Overall memory use change: 0.00195312
I'm running on Linux (SL6.6) and compiling with gcc 4.1.2 (though I've also tried clang and ICC with similar results).
Obviously, it's not a huge leak; it's just a little annoying that I can't make it completely tidy... Is there something I can/should do to release the memory manually? Or is it just something weird (with my setup and/or stringstream itself) I'll have to live with?
NB The intended use for the stringstream is to read in some file contents above and then parse them line by line; I would try using istringstream but I couldn't figure out how to set its value from the ifstream...
Thanks!
Say that I execute some command to run in the terminal within a C++ program. For instance:
int main(){
std::system("./myprog");
return 0;
}
Assume that myprog produces some output that is printed to the console. Can I make use of this output within my C++ program? For example:
int main(){
some_var = std::system("./myprog");
if (some_var == "something")
// Do something.
return 0;
}
Any help would be very much appreciated. Thanks again.
You need to use the popen function:
FILE *fp = popen("./myprog", "r");
char buffer[128];
while (fgets(buffer, sizeof(buffer), fp))
{
std::cout << "Output from program: " << buffer << '\n';
}
pclose(fp);
While reading a binary file using DJGPP on DOS this code hangs.
This happens when the fread call is made. If the call is removed then the program runs successfully.
The same code runs fine through Visual C++ 2008.
Has anyone experienced similar issues with djgpp ?
Am I missing out on something really simple ?
char x;
string Filename = "my.bin" ;
fp = fopen(Filename.c_str(),"rb");
if (fp == NULL)
{
cout << "File not found" << endl ;
}
if (fseek (fp, 0, SEEK_END) != 0)
{
cout <<"End of File can't be seeked";
return -1;
}
if ( (fileLength = ftell(fp)) == -1)
{
cout <<"Can't read current position of file";
return -1;
}
if (fseek (fp, 0, SEEK_SET) != 0)
{
cout <<"Beginning of File can't be seeked";
return -1;
}
if (fread(&x,sizeof(x),1,fp) != sizeof(x))
{
cout <<"file not read correctly";
return -1;
}
I don't see what 'fp' is. I just have to assume it's 'FILE * fp;'.
I don't see that you actually include < stdio.h >, and have to assume you do.
I don't see that you actually include < iostream > and declare 'using namespace std;', and have to assume you do.
I don't see what comes after the fread() call that could tell you if call succeeded.
Going through the process of actually reducing your faulty code to the absolute but complete minimum to reproduce the error is the first thing you should do when a piece of code has you dumbfounded.
It might (and usually does) turn out that the problem isn't even where you thought it is.
That being said, I'd try replacing
if (fread(&x,sizeof(x),1,fp) != sizeof(x))
{
cout <<"file not read correctly";
return -1;
}
with
int i;
if ( ( i = fgetc( fp ) ) == EOF )
{
perror( "File not read correctly" );
return -1;
}
x = (char) i;
cout << "Success, read '" << x << "'." << endl;
Using 'perror()' instead of homebrewn cout messages gives you additional information on the cause of any error. Using 'fgetc()' will show you that the file actually does contain what you think it does, and that your problems are not due to the somewhat uncommon use of fread() for a single byte.
Then report back.
fread takes a pointer as the first parameter. If you only have to read in one character, char x; is fine, but pass the address of x instead.
fread(&x,sizeof(x),1,fp) != sizeof(x)
and since sizeof char is always 1 (by definition) you can very well write:
fread(&x,1,1,fp) != 1