I've started debbuging on some app, which hangs up in a loop based on readdir call.
Step by step I've cut everything but problem code, this is it:
So, in basic, it shows name of first entry and nothing more. It even does not exits, just waiting for something.
Also, I've found, that if don't lin it against libpocofoundation, it works.
But I have to do it because it used in the original app.
I'm a little bit confused, I don't use Poco in this example in any way, but it some way hangs it.
Please help me, I'm in panic :D
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>
#include <string>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[])
{
const char TMP_DIR[] = "/opt";
DIR *dir = opendir(TMP_DIR);
std::cerr
<< readdir(dir)->d_name
<< readdir(dir)->d_name
<< std::endl;
return 0;
}
So... I don't know why it was happening. So I just dropped libpoco.
Related
I am trying to show in an edit box a program execution time. I have found some examples at Stackoverflow like the code here below.
using namespace std;
using namespace date;
ostringstream out;
auto start = chrono::system_clock::now();
//some program execution
auto finish = chrono::system_clock::now();
out << finish - start;
string s = out.str();
cout << s << '\n';
I have installed the library #include <date/date.h> via vcpkg. But the problem is: as soon as I do #include <date/date.h>, and run code with Local Windows Debugger a numerous mistake is happening as indicated in the snap shot below.
I mean, simple including of <date/date.h> library leads to errors.
How can I avoid this issue?
Many thanks in advance!
UPD
#include "pch.h"
#include "framework.h"
#include "MFCApplication2.h"
#include "MFCApplication2Dlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <date/date.h>
void CMFCApplication2Dlg::OnBnClickedButton1()
{
//I have cleared the code inside, but errors yet appear
}
When I started to use VS2013, I created just very basic application like this.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello!";
return 0;
}
It crashed and when I commented out the #include <iostream> its no longer crash. I did several research on this error but nothing is suitable for my situation. This is the error :
Thanks for all your helps.
Once you create a new project, if you create it as an empty project I don't think you will face this issue. Then, you start it from scratch and you use int main() instead of that _tmain(...) and DO NOT EVER use using namespace std;
start a new EMPTY project and use something like this:
#include <iostream>
int main()
{
std::cout << "Hello World";
return 0;
}
...Am trying to load/capture the output of system(char* command) function to a variable, a vector. can i have any possible way to push the output to my vector? I don*t want to write the output to file and read it again.
Sample code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
vector <string> dir;
system("pwd");//here i used this to print the current directory, and i want to store this out put to my vector. something like...(below )
output=output of system("pwd");//this is not a real code,just to notice i want to put the out put to other var and push.
dir.push_back(output);
return 0;
}
Can i have any scenario to do this task, thanks.
I'd recommend doing it like this:
FILE *fp = popen("fortune","r");
char line[200];
while(!feof(fp)) {
fgets(line,200,fp);
// process here
}
pclose(fp);
If it's really performance critical it's probably better to
create a child process using fork() and pipes for stdin/stdout of that child
process to write or read from.
An example of this could be found here (http://www.microhowto.info/howto/capture_the_output_of_a_child_process_in_c.html#idp21888) if you're intested. But the popen method is probably the most simple and straightforward one in your case.
I'm writing a batch emulator as a personal project. I'm trying to implement the cd command using chdir() from unistd.h. However, using this causes a segfault.
main.cpp:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
//Custom headers
#include "splitting_algorithm.hpp"
#include "lowercase.hpp"
#include "chdir.hpp"
//Used to get and print the current working directory
#define GetCurrentDir getcwd
using namespace std;
int main(int argc, char* argv[])
{
string command;
//Begin REPL code
while (true)
{
//Prints current working directory
cout<<cCurrentPath<<": ";
std::getline(std::cin, command);
vector<string> tempCommand = strSplitter(command, " ");
string lowerCommand = makeLowercase(string(strSplitter(command, " ")[0]));
//Help text
if(tempCommand.size()==2 && string(tempCommand[1])=="/?")
{
cout<<helpText(lowerCommand);
}
//Exit command
else if(lowerCommand=="exit")
{
return 0;
}
else if(lowerCommand=="chdir")
{
cout<<string(tempCommand[1])<<endl;
chdir(tempCommand[1]);
}
else
cout<<"Can't recognize \'"<<string(tempCommand[0])<<"\' as an internal or external command, or batch script."<<endl;
}
return 0;
}
chdir.cpp:
#include <cstdlib>
#include <string>
#include <unistd.h>
void chdir(std::string path)
{
//Changes the current working directory to path
chdir(path);
}
Strangely enough, using cout to get the path for chdir works perfectly fine. How do I fix this?
You have recursive, unterminated behaviour in Your code. This overflows the stack.
Try to insert breakpoint in void chdir(std::string path) and see what happens.
You will see that the function chdir calls itself, and in turn calls itself again, and again and... well, segmentation fault.
Also, try to see what "call stack" is in the debugger, this issue is very visible there.
You should invoke the underlying chdir function using
::chdir(path.c_str());
or you will just call your own method again.
In unistd.h, chdir is defined as:
int chdir(const char *);
So you must call it with a const char* argument or the compiler will search for another function called "chdir" which take a std::string argument and use that instead.
I'm completely new to C++, but I have created a minor program, looking to port the program to other computers, but when I "install" the program I get this error...-static-libgcc -static-libstdc++ missing, is there a file I should be including in the program itself, or is this a library I have to install on each computer? The computers that I expect to run the program will be windows xp. Source code of the file is as follows:
#include <stdlib.h>
#include <windows.h>
#include <direct.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
int main(int argc, const char *argv[])
{
_chdir("C:\\Program Files\\NCHSoftware\\Talk\\");
string number = "start talk.exe -dial " + std::string(argv[1]+4);
system(number.c_str());
exit;
return 0;
}
They are shared lib's that would need to be on the host computer.
To learn how to compile a static version;
See here: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
Read the "-static-libgcc" & "-static-libstdc++" sections.