sydneyguy
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
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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