czwartek, 21 lipca 2016

struct utsname and sysinfo in file

#include <iostream>
#include <fstream>
#include <signal.h>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
#include <stdio.h>
#define megabyte 1024*1024

using namespace std;


class Info_system
{
private:
  struct utsname my_uname;
       struct sysinfo my_info;
       FILE *my_file;
       char *my_name;
       double write_megabyte;
public:
 Info_system(char* my_file_info)
 {
 sysinfo(&my_info);
 uname(&my_uname);
 my_name=my_file_info;
 }
 void write_to_FIle ()
 {
 
 my_file=fopen(my_name,"w");
 fprintf(my_file,"%s\n","Total RAM:");
 fprintf(my_file,"%d\n",(my_info.totalram/megabyte));
 fprintf(my_file,"%s\n","Free RAM:");
 fprintf(my_file,"%d\n",(my_info.freeram/megabyte));
 fprintf(my_file,"%s\n","Shared RAM: ");
 fprintf(my_file,"%d\n",(my_info.sharedram/megabyte));
 fprintf(my_file,"%s\n","Buffer RAM:");
 fprintf(my_file,"%d\n",(my_info.bufferram/megabyte));
 fprintf(my_file,"%s\n","Total SWAP:");
 fprintf(my_file,"%d\n",(my_info.totalswap/megabyte));
 fprintf(my_file,"%s\n","Free SWAP: ");
 fprintf(my_file,"%d\n",my_info.freeswap/megabyte);
 fprintf(my_file,"%s\n","Short PROCS: ");
 fprintf(my_file,"%d\n",my_info.procs);
 fprintf(my_file,"%s\n",my_uname.sysname);
 fprintf(my_file,"%s\n",my_uname.release);
 fprintf(my_file,"%s\n",my_uname.version);
 fprintf(my_file,"%s\n",my_uname.machine);
 fclose(my_file);
}
void read_from_File()
{
my_file=fopen(my_name,"r");
char my_buffer[2048];
while(!feof(my_file) && !ferror(my_file)
&& fgets(my_buffer,sizeof(my_buffer),my_file)!=NULL)
fputs(my_buffer,stdout);
fclose(my_file);
}
   
};



int main(int argc, char** argv)
{
Info_system My_s("file_statistic");
My_s.write_to_FIle();
My_s.read_from_File();

return 0;
}

Brak komentarzy:

Prześlij komentarz