Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a file which have the following data

3550: async from 60:159 to 128:0 node 2180 handle 51 size 204:4
3552: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3553: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3554: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3555: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3556: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3557: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3558: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3559: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3560: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3561: call  from 122:122 to 60:0 node 35 handle 9 size 100:4
3562: reply from 60:182 to 122:122 node 0 handle -1 size 4:0
3563: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3564: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3565: async from 60:72 to 124:0 node 571 handle 6 size 300:4
3566: async from 124:124 to 60:0 node 138 handle 1 size 100:4
3567: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3568: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3569: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3570: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3571: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3572: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3573: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3574: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3575: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3576: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3577: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3578: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3579: async from 124:124 to 60:0 node 23 handle 6 size 64:0
3580: call  from 122:122 to 60:0 node 35 handle 9 size 100:4
3581: reply from 60:187 to 122:122 node 0 handle -1 size 4:0
3582: async from 124:124 to 60:0 node 23 handle 6 size 64:0



I want to detect the integers that are written after from .i.e.64,124.
I want Do it in C only.Any help how to do it?
Posted

1 solution

Load a line of the file into a character buffer.

Use strstr(szBuffer, "from") to find the pointer to the "from" substring.

Add strlen("from") to the pointer.

This will give you the approximate location of the text you want.

Example - Untested, just banged it out. Some debugging may be required...

C++
#include "stdio.h"

int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt", "r");
  if (pFile==NULL)
     return -1;

  char szBuffer[65536];

  while (fgets(szBuffer, sizeof(szBuffer), pFile))
  {
    char * pFromLoc = strstr(szBuffer, "from");
    if (pFromLoc == NULL)
      continue;

    char *pNumberStart = pFromLoc + strlen("from"); 

    int Number = atoi(pNumberStart);
    printf("%d\n", Number);
  }
    
  fclose (pFile);

  return 0;
}
 
Share this answer
 
v4
Comments
Tarun Batra 30-Aug-12 13:51pm    
Any sample code sir...????
enhzflep 30-Aug-12 14:48pm    
There's a code sample in Jack's post now. :)

Another (very nasty) approach would start with an assessment that the number in concern starts at the same position on each line - BUT only for the first number after the word 'from' - It would be considered bad/poor practise to extract a number from a point that was 17 bytes after the start of the line.

Removing the search step and replacing it with a simple offset would only be a valid/better solution on systems with little speed and power + other resources. An embedded micro-controller - though these days, it's likely that this approach would provide no benefit.
Tarun Batra 30-Aug-12 15:00pm    
Sir can u provide a sample code
enhzflep 30-Aug-12 15:07pm    
:confused:
What do you mean my friend?

Jack DID post a sample. You need to adapt it to suit your needs. All you need to do is change the opened file from myfile.txt to whatever your data file is called
Tarun Batra 30-Aug-12 15:10pm    
ohk now got your point

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900