vim f_access.cxx
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;
class FILE_ACCESS
{
private:
struct stat st_file;
char *file_name;
int read_value;
int write_value;
int exist_value;
bool is_True(int x);
public:
FILE_ACCESS(char *f_name);
void Report1();
void Report2();
};
bool FILE_ACCESS::is_True(int x)
{
if(x==0)
return true;
else
return false;
}
FILE_ACCESS::FILE_ACCESS(char *f_name)
{
file_name=f_name;
lstat(file_name,&st_file);
}
void FILE_ACCESS::Report1()
{
exist_value=access(file_name,F_OK);
if(is_True(exist_value))
{
cout<<file_name<<" exists "<<endl;
}
else
{
if(errno==ENOENT)
{
cout<<file_name<<" not exists "<<endl;
}
else if(errno==EACCES)
{
cout<<file_name<<" is unavailable "<<endl;
}
}
read_value=access(file_name,R_OK);
if(is_True(read_value))
{
cout<<file_name<<" is readable "<<endl;
cout<<"process "<< (int)getpid<<endl;
}
else
{
cout<<file_name<<" is not readable "<<endl;
}
write_value=access(file_name,W_OK);
if(is_True(write_value))
{
cout<<file_name<<" is writetable "<<endl;
}
else if(errno==EACCES)
{
cout<<file_name<<" is not writetable (access denied)"<<endl;
}
else if(errno==EROFS)
{
cout<<file_name<<" is not writetable (only read-only)"<<endl;
}
}
void FILE_ACCESS::Report2()
{
if(S_ISLNK(st_file.st_mode))
{
cout<<" symbolic link "<<endl;
}
else if(S_ISDIR(st_file.st_mode))
{
cout<<" directory "<<endl;
}
else if(S_ISCHR(st_file.st_mode))
{
cout<<" character device "<<endl;
}
else if(S_ISBLK(st_file.st_mode))
{
cout<<" block device "<<endl;
}
else if(S_ISFIFO(st_file.st_mode))
{
cout<<" FIFO queue "<<endl;
}
else if(S_ISSOCK(st_file.st_mode))
{
cout <<" socket "<<endl;
}
else if(S_ISREG(st_file.st_mode))
{
cout<<" plain file"<<endl;
}
}
int main(int argc, char **argv)
{
FILE_ACCESS my_ACCESS1("/bin/bash"),my_ACCESS2("f_access.cxx");
my_ACCESS1.Report1();
my_ACCESS1.Report2();
my_ACCESS2.Report1();
my_ACCESS2.Report2();
return 0;
}
środa, 7 listopada 2018
poniedziałek, 5 listopada 2018
Reading data from a file (C++)
I want to find out what happened at the given time in the system.
Windows stores error reports in the file - PFRO.
Functions for char and wchar_t types:
wchar_t* __fastcall TForm1::PFRO_Report() { wchar_t end='\0'; wchar_t *report; wchar_t *reply; wchar_t temp_buff[2048]; wchar_t *incident=L"3/18/2017 18:27:22 - PFRO Error:"; size_t byt_read_values; FILE *file; file=_wfopen(L"c:\Windows\PFRO.log",L"r+"); byt_read_values=fread(temp_buff,1,sizeof(temp_buff),file); fclose(file); temp_buff[byt_read_values]=end; report=wcsstr(temp_buff,incident); if(report==0) return 0; swscanf(report,incident,L"%s",reply); return reply; }
char* __fastcall TForm1::PFRO_Report() { char end='\0'; char *report; char *reply; char temp_buff[2048]; char *incident="3/18/2017 18:27:22 - PFRO Error:"; size_t byt_read_values; FILE *file; file=fopen("c:\Windows\PFRO.log","r"); byt_read_values=fread(temp_buff,1,sizeof(temp_buff),file); fclose(file); temp_buff[byt_read_values]=end; report=strstr(temp_buff,incident); if(report==0) return 0; sscanf(report,incident,L"%s",reply); return reply; }
niedziela, 28 października 2018
mmap-munmap (gcc Linux)
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#define max 1000
void init_Random()
{
srand(time(NULL));
}
int is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return 1;
else
return -1;
}
int *tab,fd;
void *take;
char *test_file="first_numbers2";
void init_Tab()
{
int i,j;
tab=malloc(max*sizeof(int));
i=0;
j=2;
do
{
if(is_First(j)==1)
{
tab[i]=j;
++i;
}
++j;
}while(i<max);
}
void make_File(int file)
{
lseek(file,0x100+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
}
int main(int argc, char **argv)
{
int i;
init_Random();
init_Tab();
fd=open(test_file,O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
make_File(fd);
take=mmap(0,0x100,PROT_WRITE,MAP_SHARED,fd,0);
close(fd);
for(i=0;i<max;i++)
{
sprintf((char*)take,"%d\n",tab[i]);
}
munmap(take,0x100);
return 0;
}
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#define max 1000
void init_Random()
{
srand(time(NULL));
}
int is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return 1;
else
return -1;
}
int *tab,fd;
void *take;
char *test_file="first_numbers2";
void init_Tab()
{
int i,j;
tab=malloc(max*sizeof(int));
i=0;
j=2;
do
{
if(is_First(j)==1)
{
tab[i]=j;
++i;
}
++j;
}while(i<max);
}
void make_File(int file)
{
lseek(file,0x100+1,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
}
int main(int argc, char **argv)
{
int i;
init_Random();
init_Tab();
fd=open(test_file,O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
make_File(fd);
take=mmap(0,0x100,PROT_WRITE,MAP_SHARED,fd,0);
close(fd);
for(i=0;i<max;i++)
{
sprintf((char*)take,"%d\n",tab[i]);
}
munmap(take,0x100);
return 0;
}
niedziela, 2 września 2018
pthread_create, pthread_join (gcc)
vim three_creads.c
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define max 100
struct int_params
{
int int_value;
int count;
};
struct double_params
{
double double_value;
int count;
};
struct char_params
{
char char_value;
int count;
};
int *int_tab;
char *char_tab;
double *double_tab;
void init_Tables()
{
int i;
int_tab=(int*) calloc (max,sizeof(int));
char_tab=(char*) calloc(max,sizeof(char));
double_tab=(double*) calloc(max,sizeof(double));
for(i=0;i<max;i++)
{
int_tab[i]=i+1;
char_tab[i]='=';
double_tab[i]=sqrt(1.*int_tab[i]);
}
}
void delete_Tables()
{
free(double_tab);
free(char_tab);
free(int_tab);
}
void *int_Print(void *value)
{
int i;
struct int_params *temp=(struct int_params*) value;
for(i=0;i<temp->count;i++)
{
printf("%d ",temp->int_value);
printf("\n");
}
return NULL;
}
void *char_Print(void *value)
{
int i;
struct char_params *temp=(struct char_params*) value;
for(i=0;i<temp->count;i++)
{
printf("%c ",temp->char_value);
printf("\n");
}
return NULL;
}
void *double_Print(void *value)
{
int i;
struct double_params *temp=(struct double_params*) value;
for(i=0;i<temp->count;i++)
{
printf("%.2f ",temp->double_value);
printf("\n");
}
return NULL;
}
pthread_t int_id,char_id,double_id;
int main()
{
int i;
init_Tables();
struct int_params i_P[max];
struct char_params c_P[max];
struct double_params d_P[max];
for(i=0;i<max;i++)
{
i_P[i].int_value=int_tab[i];
i_P[i].count=max;
c_P[i].char_value=char_tab[i];
c_P[i].count=max;
d_P[i].double_value=double_tab[i];
d_P[i].count=max;
}
pthread_create(&int_id,NULL,&int_Print,&*i_P);
pthread_create(&char_id,NULL,&char_Print,&*c_P);
pthread_create(&double_id,NULL,&double_Print,&*d_P);
pthread_join(int_id,NULL);
pthread_join(char_id,NULL);
pthread_join(double_id,NULL);
delete_Tables();
return 0;
}
gcc -o three_creads three_creads.c -lpthread -lm
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define max 100
struct int_params
{
int int_value;
int count;
};
struct double_params
{
double double_value;
int count;
};
struct char_params
{
char char_value;
int count;
};
int *int_tab;
char *char_tab;
double *double_tab;
void init_Tables()
{
int i;
int_tab=(int*) calloc (max,sizeof(int));
char_tab=(char*) calloc(max,sizeof(char));
double_tab=(double*) calloc(max,sizeof(double));
for(i=0;i<max;i++)
{
int_tab[i]=i+1;
char_tab[i]='=';
double_tab[i]=sqrt(1.*int_tab[i]);
}
}
void delete_Tables()
{
free(double_tab);
free(char_tab);
free(int_tab);
}
void *int_Print(void *value)
{
int i;
struct int_params *temp=(struct int_params*) value;
for(i=0;i<temp->count;i++)
{
printf("%d ",temp->int_value);
printf("\n");
}
return NULL;
}
void *char_Print(void *value)
{
int i;
struct char_params *temp=(struct char_params*) value;
for(i=0;i<temp->count;i++)
{
printf("%c ",temp->char_value);
printf("\n");
}
return NULL;
}
void *double_Print(void *value)
{
int i;
struct double_params *temp=(struct double_params*) value;
for(i=0;i<temp->count;i++)
{
printf("%.2f ",temp->double_value);
printf("\n");
}
return NULL;
}
pthread_t int_id,char_id,double_id;
int main()
{
int i;
init_Tables();
struct int_params i_P[max];
struct char_params c_P[max];
struct double_params d_P[max];
for(i=0;i<max;i++)
{
i_P[i].int_value=int_tab[i];
i_P[i].count=max;
c_P[i].char_value=char_tab[i];
c_P[i].count=max;
d_P[i].double_value=double_tab[i];
d_P[i].count=max;
}
pthread_create(&int_id,NULL,&int_Print,&*i_P);
pthread_create(&char_id,NULL,&char_Print,&*c_P);
pthread_create(&double_id,NULL,&double_Print,&*d_P);
pthread_join(int_id,NULL);
pthread_join(char_id,NULL);
pthread_join(double_id,NULL);
delete_Tables();
return 0;
}
gcc -o three_creads three_creads.c -lpthread -lm
sobota, 1 września 2018
Dynamic libraries in C (gcc)
vim my_cpp_function.so
bool test_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
int is_First(int value)
{
if(test_First(value))
return 1;
else
return -1;
}
double value_Average(int *tab, int size);
{
int i,sum;
double value;
value=1.0;
sum=0;
for(i=0;i<size;i++)
{
sum+=tab[i];
}
value=(1.*sum)/(1.*size);
return value;
}
vim test_dynamic.c
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#define max 1000
int main()
{
int *tab_First;
char *text_Error;
int is_F(int value);
double v_A(int *tab, int size);
double average;
int i,j;
#ifdef __cplusplus
extern "C"
{
#endif
int is_First (int value);
double value_Average(int *tab, int size);
#ifdef __cplusplus
}
#endif
void *handle=dlopen("my_cpp_function.so",RTLD_LAZY);
if(!handle)
{
fprintf(stderr,"%s\n",dlerror());
exit(EXIT_FAILURE);
}
dlerror();
*(void**)(&is_F)=dlsym(handle,"is_First");
if((text_Error=dlerror())!=NULL)
{
fprintf(stderr,"%s\n",text_Error);
exit(EXIT_FAILURE);
}
*(void**)(&v_A)=dlsym(handle,"value_Average");
if((text_Error=dlerror())!=NULL)
{
fprintf(stderr,"%s\n",text_Error);
exit(EXIT_FAILURE);
}
tab_First=(int*) calloc(max,sizeof(int));
if(tab_First==NULL)
exit(1);
i=0;
j=2;
do
{
if(is_F(j))
{
tab_First[i]=j;
++i;
}
++j;
}while(i<max);
average=v_A(tab_First,max);
for(i=0;i<max;i++)
{
printf("%d \n",tab_First[i]);
}
printf("Average: %.2f ",average);
printf("\n");
dlclose(handle);
free(tab_First);
return 0;
}
gcc -shared -rdynamic -o test_dynamic test_dynamic.c -ldl
bool test_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
int is_First(int value)
{
if(test_First(value))
return 1;
else
return -1;
}
double value_Average(int *tab, int size);
{
int i,sum;
double value;
value=1.0;
sum=0;
for(i=0;i<size;i++)
{
sum+=tab[i];
}
value=(1.*sum)/(1.*size);
return value;
}
vim test_dynamic.c
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#define max 1000
int main()
{
int *tab_First;
char *text_Error;
int is_F(int value);
double v_A(int *tab, int size);
double average;
int i,j;
#ifdef __cplusplus
extern "C"
{
#endif
int is_First (int value);
double value_Average(int *tab, int size);
#ifdef __cplusplus
}
#endif
void *handle=dlopen("my_cpp_function.so",RTLD_LAZY);
if(!handle)
{
fprintf(stderr,"%s\n",dlerror());
exit(EXIT_FAILURE);
}
dlerror();
*(void**)(&is_F)=dlsym(handle,"is_First");
if((text_Error=dlerror())!=NULL)
{
fprintf(stderr,"%s\n",text_Error);
exit(EXIT_FAILURE);
}
*(void**)(&v_A)=dlsym(handle,"value_Average");
if((text_Error=dlerror())!=NULL)
{
fprintf(stderr,"%s\n",text_Error);
exit(EXIT_FAILURE);
}
tab_First=(int*) calloc(max,sizeof(int));
if(tab_First==NULL)
exit(1);
i=0;
j=2;
do
{
if(is_F(j))
{
tab_First[i]=j;
++i;
}
++j;
}while(i<max);
average=v_A(tab_First,max);
for(i=0;i<max;i++)
{
printf("%d \n",tab_First[i]);
}
printf("Average: %.2f ",average);
printf("\n");
dlclose(handle);
free(tab_First);
return 0;
}
gcc -shared -rdynamic -o test_dynamic test_dynamic.c -ldl
sobota, 18 sierpnia 2018
Symmetrical drawing - C++ Builder
private: // User declarations
int centr_x,centr_y,width,height;
void __fastcall line_C(TImage *t);
void __fastcall ellipse_C(TImage *t);
void __fastcall rectangle_C(TImage *t,int x,int y, int width, int height);
void __fastcall using_R(TImage *t);
void __fastcall line_D(TImage *t);
TColor __fastcall draw_Color(int value);
void __fastcall chaos_R();
void __fastcall chaos_E();
void __fastcall left_R();
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
width=Image1->Width;
height=width;
centr_x=width/2;
centr_y=centr_x;
srand(time(NULL));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::line_C(TImage *t)
{
t->Canvas->MoveTo(0,0);
t->Canvas->LineTo(width,height);
t->Canvas->MoveTo(0,height);
t->Canvas->LineTo(width,0);
}
void __fastcall TForm1::ellipse_C(TImage *t)
{
int r=10;
int i;
for(i=r;i<width;i+=r)
{
t->Canvas->Ellipse(centr_x,centr_y,i,i);
}
}
void __fastcall TForm1::rectangle_C(TImage *t,int x,int y, int width, int height)
{
t->Canvas->MoveTo(x,y);
t->Canvas->LineTo(x+width,y);
t->Canvas->LineTo(x+width,y+height);
t->Canvas->LineTo(x,y+height);
t->Canvas->LineTo(x,y);
}
void __fastcall TForm1::using_R(TImage *t)
{
int i;
int r=10;
int x,y;
x=width-200;
y=200;
for(i=0;i<width;i+=r)
rectangle_C(t,i,height-i,r*3,r*3);
}
void __fastcall TForm1::line_D(TImage *t)
{
int i,j;
int r=5;
t->Canvas->Pen->Color=clGreen;
for(i=centr_x;i<width;i+=r)
{
t->Canvas->MoveTo(centr_x,centr_y);
t->Canvas->LineTo(i,height);
}
t->Canvas->Pen->Color=clRed;
for(j=centr_y;j<height;j+=r)
{
t->Canvas->MoveTo(centr_x,centr_y);
t->Canvas->LineTo(width,j);
}
}
TColor __fastcall TForm1::draw_Color(int value)
{
TColor color;
switch(value)
{
case 0: color=clBlack;break;
case 1: color=clRed; break;
case 2: color=clBlue;break;
case 4: color=clGreen;break;
case 5: color=clYellow;break;
case 6: color=clFuchsia;break;
case 7: color=clLime;break;
case 8: color=clPurple;break;
case 9: color=clGray;break;
default: color=clWhite;
}
return color;
}
void __fastcall TForm1::chaos_R()
{
int min,max,i,value,random;
int left,top;
min=3,max=25;
TColor temp_Color;
for(i=0;i<30;i++)
{
random=rand()%10;
value=min+rand()%(max-min);
temp_Color=draw_Color(random);
Image1->Canvas->Pen->Color=temp_Color;
Image2->Canvas->Pen->Color=temp_Color;
Image3->Canvas->Pen->Color=temp_Color;
Image4->Canvas->Pen->Color=temp_Color;
left=rand()%centr_x;
top=centr_y+rand()%centr_y;
rectangle_C(Image1,left,top,value,value);
rectangle_C(Image2,left,top,value,value);
rectangle_C(Image3,left,top,value,value);
rectangle_C(Image4,left,top,value,value);
}
}
void __fastcall TForm1::chaos_E()
{
int min,max,i,value_x,value_y,random;
int left,top;
min=3,max=25;
TColor temp_Color;
for(i=0;i<15;i++)
{
random=rand()%10;
value_x=min+rand()%(max-min);
value_y=min+rand()%(max-min);
temp_Color=draw_Color(random);
Image1->Canvas->Pen->Color=temp_Color;
Image2->Canvas->Pen->Color=temp_Color;
Image3->Canvas->Pen->Color=temp_Color;
Image4->Canvas->Pen->Color=temp_Color;
left=centr_x+rand()%centr_x;
top=rand()%centr_y;
Image1->Canvas->Ellipse(left,top,left-value_x,top-value_y);
Image2->Canvas->Ellipse(left,top,left-value_x,top-value_y);
Image3->Canvas->Ellipse(left,top,left-value_x,top-value_y);
Image4->Canvas->Ellipse(left,top,left-value_x,top-value_y);
}
}
void __fastcall TForm1::left_R()
{
int width=35;
TColor temp_Color;
int random;
random=rand()%10;
temp_Color=draw_Color(random);
Image1->Canvas->Pen->Color=temp_Color;
Image2->Canvas->Pen->Color=temp_Color;
Image3->Canvas->Pen->Color=temp_Color;
Image4->Canvas->Pen->Color=temp_Color;
rectangle_C(Image1,5,5,width,width);
rectangle_C(Image2,5,5,width,width);
rectangle_C(Image3,5,5,width,width);
rectangle_C(Image4,5,5,width,width);
random=rand()%10;
temp_Color=draw_Color(random);
Image1->Canvas->Pen->Color=temp_Color;
Image2->Canvas->Pen->Color=temp_Color;
Image3->Canvas->Pen->Color=temp_Color;
Image4->Canvas->Pen->Color=temp_Color;
Image1->Canvas->MoveTo(5,5);
Image1->Canvas->LineTo(width,width);
Image1->Canvas->MoveTo(5,width);
Image1->Canvas->LineTo(width,5);
Image2->Canvas->MoveTo(5,5);
Image2->Canvas->LineTo(width,width);
Image2->Canvas->MoveTo(5,width);
Image2->Canvas->LineTo(width,5);
Image3->Canvas->MoveTo(5,5);
Image3->Canvas->LineTo(width,width);
Image3->Canvas->MoveTo(5,width);
Image3->Canvas->LineTo(width,5);
Image4->Canvas->MoveTo(5,5);
Image4->Canvas->LineTo(width,width);
Image4->Canvas->MoveTo(5,width);
Image4->Canvas->LineTo(width,5);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
line_C(Image1);
line_C(Image2);
line_C(Image3);
line_C(Image4);
ellipse_C(Image1);
ellipse_C(Image2);
ellipse_C(Image3);
ellipse_C(Image4);
using_R(Image1);
using_R(Image2);
using_R(Image3);
using_R(Image4);
line_D(Image1);
line_D(Image2);
line_D(Image3);
line_D(Image4);
chaos_R();
chaos_E();
left_R();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
niedziela, 15 lipca 2018
Point density. StringGrid. C++ Builder
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
srand(time(NULL));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::init_first_Table()
{
int i,j,x;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
x=rand()%100+1;
if(x%2==0)
{
tab1[i][j]=1;
StringGrid1->Cells[i][j]="1";
}
else
{
tab1[i][j]=0;
StringGrid1->Cells[i][j]="O";
}
}
}
}
void __fastcall TForm1::init_second_Table()
{
int i,j;
for(i=0;i<30;i++)
for(j=0;j<30;j++)
tab2[i][j]=0;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
if((i-1>-1) && tab1[i-1][j]==1)
tab2[i][j]++;
if((i-1>-1) && (j-1>=0))
if(tab1[i-1][j-1]==1)
tab2[i][j]++;
if((j-1>-1) && (tab1[i][j-1]==1))
tab2[i][j]++;
if((i+1<=30) && (j-1>-1))
if(tab1[i+1][j-1]==1)
tab2[i][j]++;
if((i+1<=30) && (tab1[i+1][j]==1))
tab2[i][j]++;
if((i+1<30) && (j+1<30))
if(tab1[i+1][j+1]==1)
tab2[i][j]++;
if((j+1<30) && (tab1[i][j+1]==1))
tab2[i][j]++;
if((i-1>-1) && (j+1<30))
if(tab1[i-1][j+1]==1)
tab2[i][j]++;
if(tab1[i][j]==1)
tab2[i][j]++;
}
}
for(i=0;i<30;i++)
for(j=0;j<30;j++)
StringGrid2->Cells[i][j]=IntToStr(tab2[i][j]);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
init_first_Table();
init_second_Table();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
Subskrybuj:
Posty (Atom)

