Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Linux Parsing
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 30-Aug-12 8:40am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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...
 
#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;
}
  Permalink  
v4
Comments
Tarun Batra at 30-Aug-12 13:51pm
   
Any sample code sir...????
enhzflep at 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 at 30-Aug-12 15:00pm
   
Sir can u provide a sample code
enhzflep at 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 at 30-Aug-12 15:10pm
   
ohk now got your point
enhzflep at 30-Aug-12 15:22pm
   
:)
Tarun Batra at 30-Aug-12 15:40pm
   
Hello sir just tell me one more thing that if i want to return all integers that will come in " int Number" at one instant then how should i do
enhzflep at 30-Aug-12 15:48pm
   
The easiest way would be to use a std::vector of ints.
That way, you don't need to know how many lines there are - each time you read a new number, you simply push_back (put at end of array) the new value.
Here's some more info: std::vector push_back example
 
EDIT: Missed the "C" tag. If you can use C++, use a vector. If not, just make a quick and simple linked list. Each node only needs to store an int and a pointer to the next node. There's a bazillion posts/articles/tutes on making a linked list.
JackDingler at 30-Aug-12 15:51pm
   
Good answer....
JackDingler at 30-Aug-12 15:57pm
   
Or allocate an array using malloc(sizeof(int) * Count);
enhzflep at 30-Aug-12 15:59pm
   
Oh yeah, of course. Never thought of pre-scanning the file to count the number of lines. (It may even consume fewer cpu cycles than using a vector too) +5
Tarun Batra at 30-Aug-12 16:03pm
   
i can't use c++,i have to do it in C actually in am working in linux(android ndk).
enhzflep at 30-Aug-12 15:57pm
   
Thanks mate - to be fair, it always is easier to go second. (as I did)
You answer gets my +5. :)
JackDingler at 30-Aug-12 15:58pm
   
But you have to know what 'Count' is :)
Tarun Batra at 30-Aug-12 16:03pm
   
i can't use c++,i have to do it in C actually in am working in linux(android ndk).
Tarun Batra at 30-Aug-12 16:17pm
   
sir i have written some solution can u check that and please explain the realloc concept(if possible by pseudo code).
Tarun Batra at 30-Aug-12 15:58pm
   
i can't use c++
Tarun Batra at 30-Aug-12 16:23pm
   
sir i have written some solution can u check that and please explain the realloc concept(if possible by pseudo code).
Tarun Batra at 30-Aug-12 15:40pm
   
Hello sir just tell me one more thing that if i want to return all integers that will come in " int Number" at one instant then how should i do
JackDingler at 30-Aug-12 15:46pm
   
I don't understand your question.
 
You want to put them into array or something?
Tarun Batra at 30-Aug-12 15:49pm
   
ya how to put them into array as sometimes i have 16 entries in the files sometimes its around 40 and want to return the array
Tarun Batra at 30-Aug-12 15:48pm
   
ya how to put them into array as sometimes i have 16 entries in the files sometimes its around 40 and want to return the array
JackDingler at 30-Aug-12 16:03pm
   
To do it in 'C' with such small counts, the malloc technique works well.
 
Start with an initial size that seems like it'll work for most cases.
malloc() an array of that size.
Insert your numbers at those indexes.
if you run out of room, call realloc to create a new array.
 
I will not post an example this time. These important concepts to learn. The answer will sink in better if you research them yourself and write the code yourself.
Tarun Batra at 30-Aug-12 16:10pm
   
sir i have written some solution can u check that and please explain the realloc concept(if possible by pseudo code).
JackDingler at 31-Aug-12 11:17am
   
realloc() takes your existing pointer to memory and attempts to resize it. It will copy your data so it isn't lost, and give you a new pointer to your data.
 
There is lot's of documentation on the web for this.

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

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 30 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100