0

I have the following string called header: "bla bla hello, just more characters filename="myfile.1.2.doc" more characters"

I need to get the file name and the file type from this string, but my solution seems to be very messy (pseudo code) :

unsigned int end = header.find("filename=");
unsigned int end2 = header.find(" " ", end + sizeof("filename=") + 1) // how to search for ' " ' ?!

std::string fullFileName = header.substr(end +sizeof("filename=") + 1 ,end2 -1);
//now look for the first "." from the end and split by that .

how to look from the end in cpp?

2
  • using " as a " rather than a parsing token: \" Commented Jun 21, 2015 at 17:26
  • You need to escape any double quote characters inside a string literal. For example: header.find(" \" ", //...); Commented Jun 21, 2015 at 17:27

2 Answers 2

2


I think It would be better if you use regular expressions.
For example: we have more complicated string with a few file names and confusing characters like (") outside the file name.

std::string str("bla bla hello, just more characters filename=\"myfile.1.2.doc\" more characters bla bla hello, just more characters filename=\"newFile.exe\" more char\"acters");
std::smatch match;
std::regex regExp("filename=\"(.*?)\\.([^.]*?)\"");

while (std::regex_search(str, match, regExp))
{
    std::string name = match[1].str();
    std::string ext = match[2].str();
    str = match.suffix().str();
}

The first iteration gives you:
name = myfile.1.2
ext = doc
The second:
name = newfile
ext = exe

Sign up to request clarification or add additional context in comments.

2 Comments

What version in Cpp should I use? I'm trying to do it, but none of these are familiar by my compiler. The only thing I see is std::regexec() std::regcomp()
It requires c++11. What compiler do you use?
0
size_t startpos = header.find("filename=");
if (startpos != header.npos)
{ // found filename
    startpos += sizeof("filename=") - 1; // sizeof determined at compile time. 
                                         // -1 ignores the null termination on the c-string
    if (startpos != header.length() && header[startpos] == '\"')
    { // next char, if there is one, should be "
        startpos++;
        size_t endpos = header.find('\"', startpos);
        if (endpos != header.npos)
        { // found terminating ". get full file name
            std::string fullfname = header.substr(startpos, endpos-startpos);
            size_t dotpos = fullfname.find_last_of('.');
            if (dotpos != fullfname.npos)
            { // found dot split string
                std::string filename = fullfname.substr(0, dotpos); 
                //add extra brains here to remove path
                std::string filetype = fullfname.substr(dotpos + 1, token.npos);
                // dostuff
                std::cout << fullfname << ": " << filename << " dot " << filetype << std::endl;
            }
            else
            {
                // handle error
            }
        }
        else
        {
            // handle error
        }
    }
    else
    {
        // handle error
    }
}
else
{
    // handle error
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.