×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    how to search and get string from a text file

    Hi all,
    i am developing an application. I need to search the text file for a string and based on this i need to get some data...

    my text file consists of data fields like

    TextFile.txt

    IP: 10.23.34.45
    Phone1: 1234567890
    Phone2: 2013456789

    now i need to search for string "Phone1:" and get corresponding number (here 1234567890) from the text file and i need to do some manipulation on that phone number.

    please any one can help how to get that phone number. can you provide any sample code.
    please urgent i am not able to proceed further.
    satsh

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2008
    Location
    Ahmedabad, Gujarat, India
    Posts
    3,852

    Re: how to search and get string from a text file

    Quote Originally Posted by sathishkatthi View Post
    Hi all,
    i am developing an application. I need to search the text file for a string and based on this i need to get some data...

    my text file consists of data fields like

    TextFile.txt

    IP: 10.23.34.45
    Phone1: 1234567890
    Phone2: 2013456789

    now i need to search for string "Phone1:" and get corresponding number (here 1234567890) from the text file and i need to do some manipulation on that phone number.

    please any one can help how to get that phone number. can you provide any sample code.
    please urgent i am not able to proceed further.
    hello satsh

    thats very simple first read the text file get the contents into a descriptor and then operate on them.

    to give you hind please go through the Find() and locate() methods of general descriptors, that what you need.

    Regards,

  3. #3
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    77

    Re: how to search and get string from a text file

    Here is a quick code snippt to parse what you need, Not checked though. Check yourself

    Code:
    TInt fileSize;
    file.Size(fileSize);
    HBufC8 *fileBuf=HBufC8::NewL(fileSize);
    TPtr8 filePtr(fileBuf->Des());
    
    _LIT8(KToFind,"Phone1:");
    TBuf<32> numberToExtract;
    TInt pos=filePtr.Find(KToFind());
    
    if(pos!=KErrNotFound)
    {
    	pos+=fileBuf.Mid(pos).Locate(':'); 
    	pos++; //skip colon
    	TInt endPos=fileBuf.Mid(pos).Locate('\n');
    	numberToExtract.Copy(fileBuf.Mid(pos,endPos));
    	numberToExtract.TrimAll();
    }
    else
    {
    	//report not found 
    }
    
    delete fileBuf;

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Re: how to search and get string from a text file

    Hi qt_phantom and gaba88, Thanks a lot for your reply.

    I am using the sample code which u provided.
    It is giving "expression syntax error" in line 1,3,4.

    1. pos+=fileBuf.Mid(pos).Locate(':');
    2. pos++; //skip colon
    3. TInt endPos=fileBuf.Mid(pos).Locate('\n');
    4. numberToExtract.Copy(fileBuf.Mid(pos,endPos));
    5. numberToExtract.TrimAll();
    Can you please tell me what is wrong in it. It is urgent, i am not able to proceed.
    satsh

  5. #5
    Regular Contributor
    Join Date
    Aug 2005
    Location
    India
    Posts
    399

    Re: how to search and get string from a text file

    Check if you have included the proper headers.

    Also you can see if the CAknItemFinder API helps you. Latest Code example

    But this API doesn't support IP address search in supplied text editor.
    Last edited by aficianado_001; 2009-12-14 at 07:22.

  6. #6
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: how to search and get string from a text file

    Quote Originally Posted by aficianado_001 View Post
    Check if you have included the proper headers.

    Also you can see if the CAknItemFinder API helps you. Latest Code example

    But this API doesn't support IP address search in supplied text editor.
    if i am not wrong it will search in a Text File.

  7. #7
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    77

    Re: how to search and get string from a text file

    sorry my mistake, use filePtr instead of fileBuf

  8. #8
    Regular Contributor
    Join Date
    Aug 2005
    Location
    India
    Posts
    399

    Re: how to search and get string from a text file

    Quote Originally Posted by skumar_rao View Post
    if i am not wrong it will search in a Text File.
    No doubt the method you provided will search the text. Item Finder API was just an introduction to another way of searching text in Editor and sometimes useful depending upon needs.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Re: how to search and get string from a text file

    Hi qt_phantom and friends,
    Thanks a lot for help and providing sample code which helps a lot for me.
    Problem got solved and it is working fine. Now it is getting number from text file based on search string.

    Thanks for all of you and Forum Nokia.
    satsh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •