c++ file handling problem - c++

hi everybody out there i need help from u in file handling
I'm using Ubuntu as my operating system
now my c++ code has a line remove("/home/manish.yadav/Desktop/ram.txt");
last time i ran this command in C code it remove my operating system
now I'm not sure to use it or not ?
can anyone tell me proper syntax of this command so that i can use it in my code ?
is there any other way to remove files in c++ by using programs? how to do this?
how delete file using c++ program ?

try it !
unlink("/home/manish.yadav/Desktop/ram.txt");

Take a look into the reference page for std::remove on how to use it to remove files.
Concerning your OS, std::remove doesn't randomly kill OSs. Last time I tried it, the function worked as expected.

"last time i ran this command in C code it remove my operating system".
No, it didn't. Something else did, and we have no crystal ball to guess what that was.

Related

Forcing a terminal windowl of set size to open in C++

My question regards c++ and trying to create a window to ensure my output is all fit into a single line. The only way I can think to do this is to force another window to open up upon running the program and have the output appear there.
Does anyone know how to do this?
I am using XCode on my Mac. Anything Helps!
The best you can do is to adjust your code to format the line within the bounds of the terminal you are using. For this, on OSX, you will need to use (and link) against termcap (short for 'terminal capabilities')
See also this one.

getenv("HOME") returns "/root" with "sudo"

I'm writing a program on my Raspberry Pi that requires the function "getenv("HOME")" to locate "/home/pi".
However, since I'm using the "wiringPi" library that requires "sudo" to run, "getenv("HOME")" now returns "/root" as the HOME directory instead of "/home/pi".
Is there a way to locate "/home/pi" with "getenv("HOME")" while using "sudo" the run the program?
Any help will be appreciated. Thank you.
Transferring comments and response into an answer.
If you know the answer is /home/pi, why do you need getenv("HOME") to get the wrong value?
It's because getenv("HOME") is the code from another library I am trying to run, which I cannot change.
Presumably, calling setenv("HOME", "/home/pi", 1) is a bit too much like cheating, too?
setenv("HOME", "/home/pi", 1) works for me.
Why are you sure that the value you need is /home/pi? Why isn't /root correct when the program is run by root (or someone running sudo)?
This becomes mostly immaterial given that there's another unchangeable library involved.
In that case, setting the correct value for the environment variable before invoking the other library is a mostly reasonable mechanism.

How to make a gui for command-line program?

Recently, i've got interested in making a front-end for command-line program.
I guess there's two way to do it.
First one is just including source code and calling main proc with arguments
(Of course, there should be some changes in source code).
Second one, which is there's no source code and just program, is just executing program internally then reading the command line with APIs.
Though I well know about the first solution, i don't know what APIs is needed to do the second solution.
I'm talking about the APIs that get a command-line string or something like that.
See this question for information on how to run an external application; basically, you need to call CreateProcess function. I'm not sure what you mean by "reading the command line", I suppose you mean reading the output of an executed program? As for capturing an external application's output, there's already another question asking for that, you will probably find this answer most helpful.
here is a codeProject project that I have used and can handle command line arguments for you (in the setup you describe). If you are not happy with it, you can use the direct WinApi calls using CommandLineToArgvW.
Enjoy!

Moving files to Trash Can in Linux using C++

I'm trying to move (delete) a file to a Trash Can (in Linux) using C++ (also using QT4 for GUI). Unfortunately it seems to be quite difficult to do so and as far as I can tell there isn't a unified API for it.
I would like for my application to run not only on KDE but on GNOME, Xfce and other Linux desktop environments. That's why I'm searching for a more universal approach.
The best I could find so far is:
send2trash - but that's using Python/QT4 and not C++/QT4
trash-cli - this has the drawback of being a stand alone command line program and not a library
I would be happy with any approach that requires as little desktop environment specific code as possible. Or in other words that's as much independent from KDE/GNOME/Xfce components as possible.
Any help in finding a solution (if there even is one) would be greatly appreciated.
Why not find a terminal command to move the files and then call system() to run it for you inside your C++ program?
This might (I haven't tested it) be a possible one-liner in Linux to move files to the trash via the terminal. You would just pass the command as a quoted string to system() and call it in your C++ implementation.
The answer is in
http://www.freedesktop.org/wiki/Specifications/trash-spec
For every user a “home trash” directory MUST be available. Its name and location are $XDG_DATA_HOME/Trash
you only need to write C++ code move your file into such directory.
You can move files using boost file system and you can retrieve the XDG_DATA_HOME value using cstlib getenv.
As far as I know there's no standard trash can in Linux in the first place.

Issuing system commands in Linux from C, C++

I know that in a DOS/Windows application, you can issue system commands from code using lines like:
system("pause");
or
system("myProgram.exe");
...from stdlib.h. Is there a similar Linux command, and if so which header file would I find it in?
Also, is this considered bad programming practice? I am considering trying to get a list of loaded kernal modules using the lsmod command. Is that a good idea or bad idea? I found some websites that seemed to view system calls (at least system("pause");) in a negative light.
system is a bad idea for several reasons:
Your program is suspended until the command finishes.
It runs the command through a shell, which means you have to worry about making sure the string you pass is safe for the shell to evaluate.
If you try to run a backgrounded command with &, it ends up being a grandchild process and gets orphaned and taken in by the init process (pid 1), and you have no way of checking its status after that.
There's no way to read the command's output back into your program.
For the first and final issues, popen is one solution, but it doesn't address the other issues. You should really use fork and exec (or posix_spawn) yourself for running any external command/program.
Not surprisingly, the command is still
system("whatever");
and the header is still stdlib.h. That header file's name means "standard library", which means it's on every standard platform that supports C.
And yes, calling system() is often a bad idea. There are usually more programmatic ways of doing things.
If you want to see how lsmod works, you can always look-up its source code and see what the major system calls are that it makes. Then use those calls yourself.
A quick Google search turns up this link, which indicates that lsmod is reading the contents of /proc/modules.
Well, lsmod does it by parsing the /proc/modules file. That would be my preferred method.
I think what you are looking for are fork and exec.