-1

I have to create a program, that displays result of 'uptime'. how to get amount of logged on users in c++ in linux?

Tomsz Comasz
  • 123
  • 1
  • 8
  • exactly what does the number of users have to do with `uptime`? – Marc B May 27 '15 at 18:27
  • @MarcB: I imagine he wants to reproduce something like this uptime command: `jake@w700-ubuntu:~$ uptime 14:29:46 up 5:59, 2 users, load average: 0.16, 0.16, 0.24` – Jake May 27 '15 at 18:30
  • @MarcB: From the man page for 'uptime': `uptime gives a one line display of the following information. The current time, how long the system has been running, **how many users are currently logged on**, and the system load averages for the past 1, 5, and 15 minutes.` – user3553031 May 27 '15 at 18:30

3 Answers3

1

Well, if you just want to execute commands using C++, you could use:

#include <stdio.h>
#include <stdlib.h>
int main()
{
  //execute uptime command
  system("uptime");
  //one way to "count" the currently logged users
  system("users | wc -w");
  //or another way
  system("who | wc -l");
  return 0;
} 
eol
  • 23,236
  • 5
  • 46
  • 64
  • I need to send that information from serverTCP to clientTCP. I want to put that in string. Maybe there is a way to put system("uptime") result to string? – Tomsz Comasz May 28 '15 at 10:16
  • yes, check out this link http://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output – eol May 28 '15 at 21:10
0

That information is stored in /var/run/utmp. This is a binary file; see the man page for utmp(5) for details.

user3553031
  • 5,990
  • 1
  • 20
  • 40
  • As I said, read the `man` page for `utmp`. It describes how to use that file. – user3553031 Jun 04 '15 at 20:16
  • [Documentation can also be found on the Internet](http://linux.die.net/man/5/utmp). I've never actually used utmp, so I can't be much more specific. – user3553031 Jun 08 '15 at 17:58
0

I got it! Write in C file system("users | wc -w > text"); and check text.txt file. Next load that information from txt file

Tomsz Comasz
  • 123
  • 1
  • 8