• C

Input from file to buffer

I have a text file containing hex values

Text file name: value.txt

Contents of value.txt

8f 8f ff 00 22 5a 7a 8a ff 33 52 8f ff 8f 8f ff 8f 8f ff 4f 4f ff 8f 8f ff ff 8f 8f ff 8f 8f ff
4f 4f ff 8f  ff 8f 8f ff 8f 8f ff 4f 4f ff 8f 00 22 00 54 55 8f ff 8f 8f ff 8f 8f ff 4f
4f ff 8f ff 8f  ff 8f 8a ff 33 54 8f ff 8f 8f ff 8f 8f  8f ff 4f 4f ff 8f 6f ff ff 6f 6f  ff
ff 4f 4f ff 8f 8f ff ff 8f 8f 00 22 00 2f 55 8f ff 8f 8f ff 8f 8f ff 4f 8f ff 8f  ff 8f
8a ff 33 54 8f ff 8f 8f ff 8f 8f  8f ff 4f 4f ff 8f 6f ff ff 6f 6f  ff 4f ff 8f 8f ff ff 8f
8f …and so on….

I want to read in the lines of the text file to a buffer starting at 00 22 and ending 24 Bytes on.

Hence
Buffer should have values:
(1): 00 22 5a 7a 8a ff 33 52 8f ff 8f 8f ff 8f 8f ff 4f 4f
then next,the buffer should read
(2): 00 22 00 54 55 8f ff 8f 8f ff 8f 8f ff 4f 4f ff 8f ff 8f  
then next, the buffer should read
(3): 00 22 00 2f 55 8f ff 8f 8f ff 8f 8f ff 4f 8f ff 8f  ff 8f
 
and so on!!
Any Ideas???
XPUSRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
fgets ( buffer, BUF_SIZE, input_file );
do
{
     p = strchr ( buffer, 0x00 );
} while (  p != NULL  || *(p+1) != 0x22 )

if ( p!= NULL)
  //copy from this byte onwards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
XPUSRAuthor Commented:
Thanks for suggestion but I am having a problem :

Here is my code, my file is called input.txt(which contains the hex values)
---------------------------------------------------------------------------------------
#include <iostream.h>
#include <string>

int main () {
      //Open File Copy Contents Into File The Close File
      int lSize;
      char * buffer;
      //Opening File
      FILE * dFile = fopen ( "output.txt" , "rb" );
      // Obtain File Size.
      fseek (dFile , 0 , SEEK_END);
      lSize = ftell (dFile);
      rewind (dFile);
      buffer = (char*) malloc (1000);

      //Put File Contents Into Buffer
      fgets ( buffer, lSize,dFile );
      fclose(dFile);

      //Test The Buffer
      printf(buffer);


char *p;

      do
      {
     p = strchr ( buffer, 0x00 );
      
      } while (  p != NULL  || *(p+1) != 0x22 )

if ( p!= NULL)
printf(p);  

//copy from this byte onwards
  return 0;
}

--------------------------------------------------------------------------------------

The problem is that strchr(buffer. 0x00) doesn't seem to find the '00' in the text file.
And the *(p+1) doesn't ever seem to point to 0x22.  

Please have a look at it,

Thanks


0
dhyaneshCommented:
hi

I think you should use following instead of strchr.

p = strstr(buffer,"00 22");

Your code will look like:

#include <iostream.h>
#include <string>

int main () {
    //Open File Copy Contents Into File The Close File
    int lSize;
     char * buffer;
    //Opening File
    FILE * dFile = fopen ( "output.txt" , "rb" );
    // Obtain File Size.
    fseek (dFile , 0 , SEEK_END);
    lSize = ftell (dFile);
    rewind (dFile);
    buffer = (char*) malloc (1000);

    //Put File Contents Into Buffer
    fgets ( buffer, lSize,dFile );
    fclose(dFile);

    //Test The Buffer
    printf(buffer);


char *p;

p = strstr(buffer,"00 22");

if ( p!= NULL)
{
   printf(p);  

   //copy from this byte onwards
}
  return 0;
}

Dhyanesh
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

XPUSRAuthor Commented:
Thanks Dhyanesh,

It works fine but i NEED a hex reference in the code (as I am using this program for demonstrational purposes!)

such as 0x22, strsrt can't take hex, i.e if I try strstr(buffer, 0x22) or strstr(buffer, "0x22") it wont work.

Any more ideas?
0
dhyaneshCommented:
hi

I think you should then modify the way you store values in the file. You should store them as:

0x00 0x22 0x5a 0x7a ....

then strstr(buffer,"0x22") will work

Another idea is to store the values not as ascii in the file but as ACTUAL numbers. However the file will not be readable or writable by simple editors but only by HEX editors or by a C program written specially to read and write the file.
e.g. to write 0x22 in file as ACTUAL number

fputc(0x22,fp);

Dhyanesh
0
XPUSRAuthor Commented:
Thanks,

wrote file in Number format and works ...
well done Dhyanesh !!

I still am stuck on the original problem though---

I will try and explain it again.
I have a file "output.txt" which contains the following hex data
-------------------------------------------------------------------------------------------------
8f 8f ff 00 22 5a 7a 8a ff 33 52 8f ff 8f 8f ff 8f 8f ff 4f 4f ff 8f 8f ff ff 8f 8f ff 8f 8f ff
4f 4f ff 8f  ff 8f 8f ff 8f 8f ff 4f 4f ff 8f 00 22 00 54 55 8f ff 8f 8f ff 8f 8f ff 4f            
4f ff 8f ff 8f  ff 8f 8a ff 33 54 8f ff 8f 8f ff 8f 8f  8f ff 4f 4f ff 8f 6f ff ff 6f 6f  ff        
ff 4f 4f ff 8f 8f ff ff 8f 8f 00 22 00 2f 55 8f ff 8f 8f ff 8f 8f ff 4f 8f ff 8f  ff 8f          
8a ff 33 54 8f ff 8f 8f ff 8f 8f  8f ff 4f 4f ff 8f 6f ff ff 6f 6f  ff 4f ff 8f 8f ff ff 8f
8f …and so on….
-------------------------------------------------------------------------------------------------

I need to find each reference to 00 22 and store the following 24 bytes in another buffer.

such that the buffer will contain

1): 00 22 5a 7a 8a ff 33 52 8f ff 8f 8f ff 8f 8f ff 4f 4f
then next,the buffer should contain
(2): 00 22 00 54 55 8f ff 8f 8f ff 8f 8f ff 4f 4f ff 8f ff 8f  
then next, the buffer should contain
(3): 00 22 00 2f 55 8f ff 8f 8f ff 8f 8f ff 4f 8f ff 8f  ff 8f

and so on..



In my code so far I have copied the contents of the file into a buffer
--------------------------------------------
fgets ( buffer, 10000, "output.txt" );
--------------------------------------------

Then I use strchr to finds the value 00 in the buffer and then i store the reference for 00 in p...the code below doesn't work as it only finds the first "00".Is there some way i can implement an offset so that once it has found the first 00 it will go on to find the next 00 and keep going until the EOF?
-----------------------------------------------------
char *p;
do
{
     p = strchr ( buffer, 0x00 );
} while (  p != NULL  || *(p+1) != 0x22 )    
-----------------------------------------------------


I want it to continously move through the file finding each 00 22 and copy the following 24bytes into a buffer as described above.


This is really getting @ me have being trying to get this going for sometime now so any help will be appreciated...

0
dhyaneshCommented:
hi

So you have stored the actual values in the file.

You wish to copy 24 bytes continuosly.

One way could be:

char *p,ch,newbuf[1000];
count = 0;
i = 0;
ch = buffer[i]
while (ch != '\0')
{
    do
   {
        p = strchr ( buffer+i, 0x00 );
   } while (  p != NULL  || *(p+1) != 0x22 )
   
    if (p == NULL)
       break;
   
    for(j=0;j<24 && *(p+j) != '\0';j++)
    {
         newbuf[count] = *(p+j);
         count++;
    }
    ch = *(p+j);
}

Dhyanesh
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.