Using regex iterator with char *

I am trying to read a file in a buffer and then use the regex iterator. I know that I can use the C ++ line iterator with the regex iterator (constructor std::regex_iterator<std::string::iterator>), but I would like to avoid copying my buffer to the line and continue to use low-level functions to read the file (right now I'm using open()and read()).

struct stat buff;
int file = open(argv[1], O_RDONLY);
if(!file)
    cout << "Error opening file" << endl;
else if(fstat(file, &buff))
    cout << "Error" << endl;
else
{
    cout << (buff.st_size) << endl;
    char fr[buff.st_size+1];

    read(file, fr, buff.st_size); // using string::c_str() or string::data() didn't work
    fr[buff.st_size] = '\0';
    // then use regex iterator to iterate through matches
}
close(file);

I think my options are to find a way to use read()with a C ++ string instead of char * or a way to use the regex iterator in a char array. I could write one, but I also try to keep my program as small as possible.

Is there any way I can do this? How to use C ++ string as C char * (for read())?

+4
2

std::regex_iterator<char*>. - . , char , . :

std::unique_ptr<char[]> fr = new char[buff.st_size + 1];
+4

std::string, read() :

    struct stat buff;
    int file = open(argv[1], O_RDONLY);
    if(!file)
        cout << "Error opening file" << endl;
    else if(fstat(file, &buff))
        cout << "Error" << endl;
    else
    {
        cout << (buff.st_size) << endl;
//      char fr[buff.st_size+1];

        std::string fr; // use a std::string
        fr.resize(buff.st_size); // resize it to create internal buffer
        read(file, &fr[0], fr.size()); // this should work

//      read(file, fr, buff.st_size); 
//      fr[buff.st_size] = '\0';
        // then use regex iterator to iterate through matches
    }
    close(file);
0

All Articles