czwartek, 27 grudnia 2018

Reading the parameter of the file (class C++)

#include <cstdlib>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <conio.h>

using namespace std;

class read_Value
{
 private:
   float float_Result;
   char *text_Result;
   char *file_Name;
   char *dest_Name;
   bool test_Buffer(size_t s_read, char *buff);
   bool is_Null(char *buff);    
   float read_float_Value(char *file_Name, char *float_Value);
   char* read_text_Value(char *file_Name,char *text_Value);
 public:
   read_Value(char *file,char *ex_result); 
   char *write_Text();
   float write_Float();   
        


};
read_Value::read_Value(char *file,char *ex_result)
{
 file_Name=file;
 dest_Name=ex_result;
          
}
bool read_Value::test_Buffer(size_t s_read, char *buff)
{
  if(s_read==0 || s_read==sizeof(buff))
return true;
else
     return false;

}
bool read_Value::is_Null(char *buff)
{
 if(buff==NULL)
return true;
else
     return false;    
}
char* read_Value::read_text_Value(char *file_Name,char *text_Value)
{
  char *result;
char temp[2048],*match_buff,*sscan_buff;
strcpy(sscan_buff,text_Value);
strcat(sscan_buff," : %s");
size_t byt_read;
FILE *f_File;
f_File=fopen(file_Name,"r");
byt_read=fread(temp,1,sizeof(temp),f_File);
fclose(f_File);
if(test_Buffer(byt_read,temp))
return 0;
temp[byt_read]='\0';
match_buff=strstr(temp,text_Value);
if(is_Null(match_buff))
return 0;
sscanf(match_buff,sscan_buff,result);
return result;     

float read_Value::read_float_Value(char *file_Name, char *float_Value)
{
  float result;
char temp[2048],*match_buff,*sscan_buff;
strcpy(sscan_buff,float_Value);
strcat(sscan_buff," : %f");
size_t byt_read;
FILE *f_File;
f_File=fopen(file_Name,"r");
byt_read=fread(temp,1,sizeof(temp),f_File);
fclose(f_File);
if(test_Buffer(byt_read,temp))
return 0;
temp[byt_read]='\0';
match_buff=strstr(temp,float_Value);
if(is_Null(match_buff))
return 0;
sscanf(match_buff,sscan_buff,&result);
return result;     
}
char * read_Value::write_Text() 
{
  text_Result=read_text_Value(file_Name,dest_Name);
  return text_Result;    
}
float read_Value::write_Float()
{
  float_Result=read_float_Value(file_Name,dest_Name);
  return float_Result;     
}
int main(int argc, char *argv[])
{
    char *file_Char;
    float file_Float;
    read_Value r_Value("C:\Nowy folder\test_File.txt","test");
    file_Char=r_Value.write_Text();
    cout<<file_Char<<endl;
    read_Value f_Value("C:\Nowy folder\float_File.txt","test");
    file_Float=f_Value.write_Float();
    cout<<file_Float<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Brak komentarzy:

Prześlij komentarz