Solved

Input from file to buffer

Posted on 2003-10-23
7
230 Views
Last Modified: 2010-05-19
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???
0
Comment
Question by:XPUSR
  • 3
  • 3
7 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 100 total points
ID: 9607665
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
 

Author Comment

by:XPUSR
ID: 9615191
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
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9616357
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:XPUSR
ID: 9616539
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
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9616732
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
 

Author Comment

by:XPUSR
ID: 9617184
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
 
LVL 4

Assisted Solution

by:dhyanesh
dhyanesh earned 250 total points
ID: 9618786
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now