How to use graphviz neato from within C++ program [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Contination of this question
#include <boost/graph/graphviz.hpp>
#include <boost/graph/grid_graph.hpp>
typedef boost::grid_graph<2> Grid;
int main()
{
boost::array<std::size_t, 2> lengths = { { 3, 5 } };
Grid grid(lengths);
std::ofstream gout;
gout.open("test.dot");
boost::write_graphviz(gout, grid);
}
I run
system('neato -Tpng overlap=false test.dot > test.png');
from a c++ program. It is not working.i.e png file is not created
When I run the same command from a console prompt, it does work as expected.

If redirection doesn't work on your system's shell, use the option:
system("neato -Tpng overlap=false test.dot -o test.png");
Also be aware of your working directory. Make sure your input is in the current working directory, and also check that you are looking for the output (test.png) in that same directory.
Alternatively, spell out the paths
system("neato -Tpng overlap=false /path/to/dir/test.dot -o /path/to/dir/test.png");
CAVEAT: of course, in C++ strings backslashes need to be escaped, if your paths contains them

Related

How to display asterisks (***) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to create an ATM application using c++ and I want to display asterisks (****) when I input the pin code. I haven't tried anything because I have no idea on how to do it.
Could someone help me?
A very small program which should help you get started for Windows would go like this:
int main(void)
{
char character = 0;
while (true)
{
character = _getch();
if (character == 13) //Enter
break;
else
{
putc('*');
//Do whatever
}
}
}
Please note that this code is off the top of my head and still requires the correct includes, but should point you in the right direction.
Please refer to https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-160 for the documentation of _getch and http://www.asciitable.com/ for a list of control codes you might be getting.

How is this program running in debug mode? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am confused about something I saw today
In a Linux terminal the next commands were given:
g++ hw.cpp
./a.out
./a.out debug
The second command executes the program and outputs "Hello, World"
The third command seems to run the program in a debug mode and makes the same program output "sending output to terminal" "Hello, World"
I did not see the source code
How can I make my own program only output certain lines in debug mode?
The source is in hw.cpp, which obviously is using some kind of argument parsing to look for 'debug'.
For example:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc > 1 && strcmp(argv[1], "debug") == 0)
printf("Hello World\n");
return 0;
}
If you would like to do argument parsing more involved then this I suggest you look to getopt

Why file is not created in /etc/ directory [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Please find the code sample
void createFile(const std::string& FileName, const std::string& Content)
{
ofstream of(FileName.c_str());
of<<Content;
of.close();
}
const std::string testFile = "/etc/testFile";
const std::string EmptyContent = "";
createFile(testFile, EmptyContent);
File is not creating at /etc/ directory. I think this is related to permissions. What extra I have to add in the code to work.
There's nothing extra that you can add to this program to "make it work". If an arbitrary program can write to /etc, this would toss the traditional POSIX security model out the window.
In order to be able to write to /etc, your program must be executed as root.
It seems to be a permission issue. Try to run your program using sudo:
sudo yourprogram

Can I only write last 50 lines console output to a file automatically [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to only write the last 50 lines of console output to a file automatically?
So that the file is always overwritten by the latest 50 lines.
You can use shell to modify and redirect the output of your program :
my_program | tail -n 50 > my_file
use simple redirection > if you want to truncate the file or double redirection >> if you want to append it.
Note :
This method only redirect stdout if you need to redirect stderr put 2>&1 after my_program
If you really want to do this in C++, you could store your console output line by line in a container while your program is running and write the last 50 lines to your file when needed.
Write a wrapper for your output function and each time you print a line to the console, add it to a std::queue. If that makes your queue larger than 50 elements, pop the oldest one (just call pop()).
class Logger {
static std::queue<string> lastFifty;
public static void log(const std::string& str) {
lastFifty.push(str);
if (lastFifty.size() > 50) {
lastFifty.pop();
}
std::cout << str;
}
public static void dumpToFile(std::ofstream& file) {
while (!lastFifty.empty()) {
file << lastFifty.pop();
}
}
}

How to change filename alongwith ext in C/CPP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have hundreds of files in a folder like:
mp_12345.dat
mp_23455.dat
mp_12323.dat
mp_44445.dat
.
.
.
I want to rename all file to another folder:
Ind_somecircle_mp_12345.mbin
Ind_somecircle_mp_23455.mbin
Ind_somecircle_mp_12323.mbin
Ind_somecircle_mp_44445.mbin
.
.
And so on.
source folder: /home/dir1/foo/
destination folder: /home/dir2/foo/
I am looking for C or C++ code to do so.
Thanks in advance.
Actually this would be a better job for a shell script, but if you insist on C then rename(src, tgt); is what you are looking for.
If you want to read the filenames from the directory, you must use opendir and readdir to loop through the files.
A short sample:
void main(int c,char **args)
{
DIR *dir;
struct dirent *dent;
dir = opendir("mydir);
if(dir!=NULL)
{
while((dent=readdir(dir))!=NULL)
printf(dent->d_name); <-- rename
}
close(dir);
}
If you insist on using C/C++ for the job you need to use
opendir - http://linux.die.net/man/3/opendir, readdir - http://linux.die.net/man/3/readdir to get the list of files
That use rename - http://linux.die.net/man/2/rename to change the filename of a file