zoukankan      html  css  js  c++  java
  • Linux-C-GetUserName

    Linux-C-GetUserName

    1. code
    //getUserName.c
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #ifdef linux
    	#include <unistd.h>
    	#include <pwd.h>
    #endif
    #ifdef _WIN32
    	#include <Windows.h>
    #endif
    
    std::string getUserName()
    {
    #ifdef linux
    	uid_t userid;
    	struct passwd* pwd;
    	userid = getuid();
    	pwd = getpwuid(userid);
    	return pwd->pw_name;
    #elif _WIN32
    	const int MAX_LEN = 100;
    	char szBuffer[MAX_LEN];
    	DWORD len = MAX_LEN;
    	if(GetUserName(szBuffer,len))
    	return szBuffer;
    #endif
    	return "";
    }
    
    int main()
    {
    	string name = getUserName();
    	cout << "Hello World!" << name << endl;
    	return 0;
    }
    
    1. 编译运行
    g++ getUserName.c
    ./a.out
    
  • 相关阅读:
    TeX中的引号
    竖式问题
    蛇形填数
    开灯问题
    排列
    分数化小数
    子序列的和
    cookie
    post请求
    get请求
  • 原文地址:https://www.cnblogs.com/yongchao/p/14253142.html
Copyright © 2011-2022 走看看