Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag for Australia

asked on

C++ finding a sting in a char* string from a text file

i have successfully retrieved the data that i want into the  'file_contents' this is then passed of to
SelectCodeToPharse (file_contents);  this is were i need to find were the text in this example "find this data" starts, so if there is  1000 characters in 'file_contents'  it may find them start at 125, i was then going to look for the term "find second data". and were it starts say at 250 then i can extract the data between 125 and 250 and then manipulate this data.
have tried several different code segments but just cannot get it going, any help would be appreciated

main
{
        char* file_contents = 0;
      char* file_contentsCulled = 0;
      file_contents = GetInternetFile(TEXT("https://www.XYZ.com"), 1000000, &sz);
      file_contentsCulled = SelectCodeToPharse (file_contents);
}

#include "stdafx.h"
#include <iostream>       // std::cout
#include <string>              // std::string
#include <cstddef>
#include <stdlib.h>

using namespace std;

// Given the file contents come back with the slab contents that were found
// between the first line and the second line
char* SelectCodeToPharse (char file_contents[10000])
{

       s = strstr(file_contents, "find this data");      // search for string "hassasin" in buff
       if (s != NULL)                     // if successful then s now points at "hassasin"
       {
              printf("Found string at index = %d\n", s - buff);
       }                                  // index of "hassasin" in buff can be found by pointer subtraction
        else
       {
              printf("String not found\n");  // `strstr` returns NULL if search string not found
        }

}
SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sydneyguy

ASKER

this is the modified code that actually works, though this project  had a second problem in that i was accidentally resetting the file_contents to the first string of the file so the data i was looking for was never there in the first place to find, so the number of examples i tried never would find the returhttps://www.experts-exchange.com/questions/28950051/C-finding-a-sting-in-a-char-string-from-a-text-file.html#ned data cause it was not there to find. so cleared the reset and it now has data to find and works well so thanks for th extra set of eyes


int  index1;
char *s1;
char *s2;
char len1 = strlen("href");
s1 = strstr(file_contents, "href");      // search for string "hassasin" in buff
cout<< file_contents <<"\n";
if (s1 != NULL)                     // if successful then s now points at "hassasin"
{
      
    index1 = s1 - file_contents;
    printf("Found string at index = %d\n", index1 );

    // Start search for 2nd string after the first string
    s2 = strstr(file_contents + index1 + len1, "hassasin");
}                                  // index of "hassasin" in file_contents usi