I want to use the system function to get the number of accounts on windows and on linux. I have no idea where to look. Please just point me in the right direction.
问题:
回答1:
The system()
function runs a program. If you know a command line that does what you need, you can use system()
to run that command line.
I'm not sure what command-line program would give the number of accounts on Windows. You could get an approximation by looking at the number of home directories. On Windows the home directories are in \Users
and on Linux home directories are in `/home'.
The system()
function doesn't capture the output of the program. You would then likely need to run a command line that redirects the program output to a file, then open this file and parse the output.
You would probably have an easier time solving this problem using a language like Python. Python programs are very portable and there are some wrappers for system stuff.
Good luck.
回答2:
I don't know in Linux, but on windows:
NetUserEnum() or NetQueryDisplayInformation() and ofcourse from the Registry here: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
PS: system(const char *command)
function call executes the command
in the command window/terminal.
回答3:
I used Google to find the following:
Windows: http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/8c72b948-d32c-4785-930e-0d6fdf032ecc
Linux: http://www.linuxforums.org/forum/miscellaneous/29508-list-all-user-accounts-system.html
(search strings: "win32 get user account information", "linux get user account information")
The Linux page gives a command line, so you can put that in your system()
call. In Windows though, you don't use system()
, you use Win32 API.
回答4:
For Linux systems you might like to count the number of lines in the file /etc/passwd
. This file contains an entry for each user to the system.
To count lines of a text file under Linux the wc
command can be used.
Anyway, if you need this info in a C program I propose you take a different approach:
You could open a text file using fopen()
and read each line using fgets()
until fgets()
tells you there are no more lines. Doing so you'll be getting the number of users.