How can I use non-default delimiters when reading a text file with std::fstream?

前端 未结 2 1693
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 05:01

In my C++ code, I want to read from a text file (*.txt) and tokenize every entry. More specifically, I want to be able to read individual words from a file, such as \"format

2条回答
  •  -上瘾入骨i
    2020-11-30 05:32

    You can use

    istream::getline(char* buffer, steamsize maxchars, char delim)
    

    although this only supports a single delimiter. To further split the lines on your different delimiters, you could use

    char* strtok(char* inString, const char* delims)  
    

    which takes multiple delimeters. When you use strtok you only need to pass it the address of your buffer the first time - after that just pass in a null and it will give you the next token from the last one it gave you, returning a null pointer when there are no more.

    EDIT: A specific implementation would be something like

    char buffer[120]; //this size is dependent on what you expect the file to contain
    while (!myIstream.eofbit) //I may have forgotten the exact syntax of the end bit
    {
        myIstream.getline(buffer, 120); //using default delimiter of \n
        char* tokBuffer;
        tokBuffer = strtok(buffer, "'- ");
        while (tokBuffer != null) {
            cout << "token is: " << tokBuffer << "\n";
            tokBuffer = strtok(null, "'- "); //I don't need to pass in the buffer again because it remembers the first time I called it
        }
    }
    

提交回复
热议问题