How to get user name from cpp in Cmake project? - c++

I am trying to just get user name. i.e., it's equivalent to whoami in ubuntu machine. But I am unable to get. I have tried following snippets.
method-1:
std::string get_username() {
struct passwd *pwd = getpwuid(getuid());
if (pwd)
return pwd->pw_name;
else
return "(?)";
}
method-2:
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
char text[255];
FILE *name;
name = popen("whoami", "r");
fgets(text, sizeof(text), name);
cout << "Name is : " << text;
pclose(name);
cout << endl;
return 0;
}
method-3:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
cout << getenv("USER") << endl;
cout << getenv("HOME") << endl;
return 0;
}
The all methods are returning value as I expected. But, When I integrate this code into my Cmake project, it always returns root. I am confused why I am always getting root as response when I try with Cmake.
How to get right value instead of root?

Related

Printing the contents of an user inserted direcory path, using dirent in C++

Total newbie in c++
Here's my problem. I am trying to write a c++ prog that reads a path from a user and prints the contents of the direcory on the screen. I am able to read a directory and print it but I cannot make it read from the outside.
#include <iostream>
#include <dirent.h>
using namespace std;
int main()
{
struct dirent *entry;
int files = 0;
DIR *folder;
const char *path;
path = userdir();
folder = opendir(path);
if (folder == NULL)
{
cout << "Unable to open the directory \n\n";
return(1);
}
else
{
cout << "Directory opened\n\n";
}
while ((entry=readdir(folder)))
{
files++;
cout << "File " << files << " " << entry->d_name << endl;
}
closedir(folder);
return(0);
}
I just want to create a function that reads the path from the user and passes the value to the main function, but somehow the fact that "path" is a const char doesn't allow me to do it.
Apologies for my ignorance...my experience in c++ is just 2 days....
OK
I managed to get a bit further, and added the following funcion
char * userdir()
{
char * ppath;
cout << "\nInsert path\n";
getline(cin,ppath);
return ppath;
}
And now the error I get is :
In function 'char* userdir()':
[Error] no matching function for call to 'getline(std::istream&, char*&)'
NB: funcion placed before int main()
Since this is C++, you can do the following to read the path:
cin >> path;

How to redirect the input and ignore the output using popen?

The files I am using look like this:
#include <iostream>
#include <windows.h>
#include <stdio.h>
using namespace std;
int main()
{
char buffer[] = "file.txt\n";
FILE *fp = popen("input.exe", "w");
fwrite(buffer, sizeof(char), sizeof(buffer), fp);
pclose(fp);
}
Which calls the compiled program:
#include <iostream>
using namespace std;
int main()
{
string path;
cout << "Enter path ";
cin >> path;
cout << "You entered " << path << endl;
}
The output I get from running the program is:
Enter path You entered file.txt
I am redirecting the input using popen, but how can I ignore the cout << "Enter path"; as well?

Relocating and renaming file C++ using stdio.h

I am trying to rename a file with the rename() function of stdio.h and it works but the problem is that it can only rename files located in the folder of the current project, I would like to be able to select a directory and if it is possible to change it from location in the process.
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main()
{
bool verifier;
char oldName[] = "text.txt";
char newName[] = "newText.txt";
verifier = rename(oldName, newName);
if (!verifier)
{
std::cout << "The file has been succesfully renamed\n";
}
else
{
std::cout << "There was a problem renaming the file\n";
}
return 0;
}
Thank you!
By default, the root directory path is the location which the executable is running in. If you want to access another folder above our outside that location, you can use an absolute path (ie C:/path/to/old.txt).
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main()
{
char oldName[] = "C:\\path\\to\\your\\proj\\text.txt"; // char oldName[] = "old.txt";
char newName[] = "C:\\test\\output\\folder\\new.txt"; // char newName[] = "newText.txt";
bool verifier = rename(oldName, newName);
if (!verifier)
{
std::cout << "The file has been succesfully renamed\n";
}
else
{
std::cout << "There was a problem renaming the file\n";
}
return 0;
}

ncurses getch() weird output?

I have installed the ncurses.h lib and started experimenting with the getch() function.When I built and run this code which seems alright to me at first, the console printed out a weird character: '�'(if it doesn't show and shows as a space here is a screen shot: https://prnt.sc/gbrp7b) The console starts spamming it but if I type a character, it shows up in the output but still the '�' spams. Here is the code:
#include <iostream>
#include <fstream>
#include <ncurses.h>
using namespace std;
int main(){
char input;
while(true){
input = getch();
cout << "You entered : " << input << endl;
//break;
}
return 0;
}
So I thought of trying to use an if statement to try and stop it spamming but the code doesn't recognise the character:
It gives this error:
error: character too large for enclosing character literal type
For this code:
#include <iostream>
#include <fstream>
#include <ncurses.h>
using namespace std;
int main(){
char input;
while(true){
input = getch();
if(input!='�'){
cout << "YOu entered : " << input << endl;
}
}
return 0;
}
I am on OSX Sierra 10.12.5 and using eclipse Oxygen
You need to initialize ncurses with initscr() and close it with endwin() functions:
#include <iostream>
#include <fstream>
#include <ncurses.h>
using namespace std;
int main(){
char input;
initscr();
while (true) {
input = getch();
cout << "YOu entered : " << input << endl;
}
endwin();
return 0;
}

How to show terminal in linux application?

I was wondering if you could have it so when you go and click on a program in linux it always automatically brings up the command line for the information being displayed or if I decided to use ncurses for an interface. If so is this a system specific call or can you do this with ncurses? Because half of my program is going to be via terminal.
Thanks
Since nitt wouldn't let me amend his code snippet, I'm posting a corrected snippet in case anyone would like to use it:
#include <cstdio>
#include <unistd.h>
#include <iostream>
int main(int argc, char* argv[])
{
if (isatty(0))
{
std::cout << "Hello, World!" << std::endl;
for (int i=0; i<argc; i++)
std::cout << "arg: " << i << "\t" << argv[i] << std::endl;
std::cout << "Press return to continue . . ." << std::flush;
std::cin.get();
}
else
{
const char* args[argc+3], **it=args;
*it++ = "gnome-terminal";
*it++ = "-x";
it = std::copy(argv, argv+argc, it);
*it++ = 0;
if (-1 == execvp("gnome-terminal", (char* const*) &args[0]))
perror("exec");
}
}
Yes, just invoke a terminal with your app in it. For example:
rxvt -e myapp
Starts a terminal running your app. You could also use xterm. If you want to use wide chars/unicode I recommend rxvt-unicode.
You can put this in a .desktop file with an icon defined there, and then that will be placed in the system menu.
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int fileExists(string x321) {
ifstream x123 (x321.c_str());
string x213;
x123 >> x213;
if (x213 == "") {
return false;
} else {
return true;
}
}
int createConsole(string fname) {
if (fileExists("~tmp") == false) {
ofstream tmp ("~tmp");
tmp << "tmpfile";
fname = "gnome-terminal -e " + fname;
system(fname.c_str());
system("exit");
return 0;
}
remove("~tmp");
return 1;
}
int main(int argc, char** args) {
createConsole(args[0]);
cout << "Hello, World!" << endl;
cout << "Press return to continue . . .";
cin.get();
}
Pay attention to the "createConsole" and "fileExists" function. I wrote this myself.