F
FireHead
Hello C World & Fanatics
I am trying replace fgets and provide a equavivalant function of
BufferedInputReader::readLine.
I am calling this readLine function as get_Stream.
In the line 4 where default_buffer_length is changed from 4 --> 24 code
works fine.
But on the same line if I change the value of default_buffer_length
from 4 --> 10 and I get a memory error.
And if the change the value of the same variable from 4 --> 1024 bytes;
the code just fails. It does not display an content on the file/ stdout
stream.
I am trying return the pointer of chars from the line from the object
variable ' temp ' into the pointer of chars variable ' result '; and I
do not know how to do this in the line 50 or 51.
And a last question
If I try to return copy the object variable ' temp ' memory address to
the ' result ' memory address I will have to call the function free in
the line 80 or so.
I want to avoid calling function free in the line 80 or so; because
this way some other user can use this code.
Current Code:
short check_Bits(int decimal){
int mask=sizeof(char)*8; /* 8 bits */
short bit = 0;
for(mask,bit = 0;mask; bit = (( mask & decimal) ? 1:0),mask = mask >>
1);
return bit;
}
1 short get_Stream(FILE* pSource,char* result){
2 char* final_buffer;final_buffer=0;
3 int default_buffer_length;
4 default_buffer_length=4; /* nibble */
5 int buffer_length = default_buffer_length;
6 char *temp;temp = 0;
7 char byte = 0;
8 int temp_length= 0;
9 /*allocate 1024 bytes to buffer object*/
10 char * buffer;buffer = 0;buffer = (char
*)malloc(default_buffer_length); /*current fgets line */
11 temp = (char *)malloc(buffer_length+default_buffer_length); /*
temporary line + current buffer */
12 /* read the file contents */
13 do{
14 fgets(buffer,default_buffer_length+1,pSource);
15 temp_length = strlen(temp);
16 /* append text */
17 if(temp_length == buffer_length){
18 buffer_length = buffer_length+default_buffer_length;
19 final_buffer = (char *)malloc(buffer_length);
20 final_buffer = strcpy(final_buffer,temp);
21 free(temp);temp = 0;
22 temp = (char
*)malloc(buffer_length+default_buffer_length);
23 temp = strcpy(temp,final_buffer);
24 free(final_buffer);final_buffer = 0;
25 }
26
27 if(temp_length == 0)
28 temp = strcpy(temp,buffer);
29 else
30 temp = strcat(temp,buffer);
40 byte = buffer[default_buffer_length-(check_Bits(buffer_length) ==
0? 2:1)];
41 }while((byte != '\n' && byte != '\r') && byte!=0 && byte!=0xFF);
42 free(buffer);
43 /* free the contents */
44 buffer_length = buffer_length-(check_Bits(strlen(temp)) == 1? 2:1);
45 temp[buffer_length]='\0';
45 short ret=EXIT_SUCCESS;
47 if(byte==0xFF || byte==0 || byte==EOF)
48 ret = EXIT_FAILURE;
49 else {
50 ret = EXIT_SUCCESS;
51 printf("%s\nDone",temp);
52 }
53 /* free the contents */
54 free(temp);
55 return ret;
56 }
57 /*End of Function */
58
59 void main(int argc,const char* argv[]){
60
61 FILE* pSourceFile=0;
62 const char* ATOMIC_NAME;
63
64 ATOMIC_NAME = "/home/generic/algorith.txt";
65 /*ATOMIC_NAME = "/usr/share/magic";*/
66
67 printf("%s\n",ATOMIC_NAME);
68 pSourceFile = fopen(ATOMIC_NAME,"rb");
69
70 if(pSourceFile==NULL){
71 printf("File not found");
72 exit(EXIT_FAILURE);
73 }
74 else
75 fseek(pSourceFile,SEEK_SET,SEEK_SET);
76
77 char* ret = "";
78 if(feof(pSourceFile)==EXIT_SUCCESS)
79 if(get_Stream(pSourceFile,ret)==EXIT_SUCCESS)
80 printf("<2>\n%s<1>",ret);
81 fclose(pSourceFile);
82 }
Can somebody help.
I will use these same function on HP-UX platform later on so therefore
I cannot use a function getline which is already available in GCC but
not available on the HP-UX platform.
Thanks.
I am trying replace fgets and provide a equavivalant function of
BufferedInputReader::readLine.
I am calling this readLine function as get_Stream.
In the line 4 where default_buffer_length is changed from 4 --> 24 code
works fine.
But on the same line if I change the value of default_buffer_length
from 4 --> 10 and I get a memory error.
And if the change the value of the same variable from 4 --> 1024 bytes;
the code just fails. It does not display an content on the file/ stdout
stream.
I am trying return the pointer of chars from the line from the object
variable ' temp ' into the pointer of chars variable ' result '; and I
do not know how to do this in the line 50 or 51.
And a last question
If I try to return copy the object variable ' temp ' memory address to
the ' result ' memory address I will have to call the function free in
the line 80 or so.
I want to avoid calling function free in the line 80 or so; because
this way some other user can use this code.
Current Code:
short check_Bits(int decimal){
int mask=sizeof(char)*8; /* 8 bits */
short bit = 0;
for(mask,bit = 0;mask; bit = (( mask & decimal) ? 1:0),mask = mask >>
1);
return bit;
}
1 short get_Stream(FILE* pSource,char* result){
2 char* final_buffer;final_buffer=0;
3 int default_buffer_length;
4 default_buffer_length=4; /* nibble */
5 int buffer_length = default_buffer_length;
6 char *temp;temp = 0;
7 char byte = 0;
8 int temp_length= 0;
9 /*allocate 1024 bytes to buffer object*/
10 char * buffer;buffer = 0;buffer = (char
*)malloc(default_buffer_length); /*current fgets line */
11 temp = (char *)malloc(buffer_length+default_buffer_length); /*
temporary line + current buffer */
12 /* read the file contents */
13 do{
14 fgets(buffer,default_buffer_length+1,pSource);
15 temp_length = strlen(temp);
16 /* append text */
17 if(temp_length == buffer_length){
18 buffer_length = buffer_length+default_buffer_length;
19 final_buffer = (char *)malloc(buffer_length);
20 final_buffer = strcpy(final_buffer,temp);
21 free(temp);temp = 0;
22 temp = (char
*)malloc(buffer_length+default_buffer_length);
23 temp = strcpy(temp,final_buffer);
24 free(final_buffer);final_buffer = 0;
25 }
26
27 if(temp_length == 0)
28 temp = strcpy(temp,buffer);
29 else
30 temp = strcat(temp,buffer);
40 byte = buffer[default_buffer_length-(check_Bits(buffer_length) ==
0? 2:1)];
41 }while((byte != '\n' && byte != '\r') && byte!=0 && byte!=0xFF);
42 free(buffer);
43 /* free the contents */
44 buffer_length = buffer_length-(check_Bits(strlen(temp)) == 1? 2:1);
45 temp[buffer_length]='\0';
45 short ret=EXIT_SUCCESS;
47 if(byte==0xFF || byte==0 || byte==EOF)
48 ret = EXIT_FAILURE;
49 else {
50 ret = EXIT_SUCCESS;
51 printf("%s\nDone",temp);
52 }
53 /* free the contents */
54 free(temp);
55 return ret;
56 }
57 /*End of Function */
58
59 void main(int argc,const char* argv[]){
60
61 FILE* pSourceFile=0;
62 const char* ATOMIC_NAME;
63
64 ATOMIC_NAME = "/home/generic/algorith.txt";
65 /*ATOMIC_NAME = "/usr/share/magic";*/
66
67 printf("%s\n",ATOMIC_NAME);
68 pSourceFile = fopen(ATOMIC_NAME,"rb");
69
70 if(pSourceFile==NULL){
71 printf("File not found");
72 exit(EXIT_FAILURE);
73 }
74 else
75 fseek(pSourceFile,SEEK_SET,SEEK_SET);
76
77 char* ret = "";
78 if(feof(pSourceFile)==EXIT_SUCCESS)
79 if(get_Stream(pSourceFile,ret)==EXIT_SUCCESS)
80 printf("<2>\n%s<1>",ret);
81 fclose(pSourceFile);
82 }
Can somebody help.
I will use these same function on HP-UX platform later on so therefore
I cannot use a function getline which is already available in GCC but
not available on the HP-UX platform.
Thanks.