我正在尝试从文件中读取特定数量的单词。
我写了这篇文章,但看来我做得不好!我很想知道是否有更好的方法。
这是我的代码:

FILE* filePointer;
wstring inputString = L"";
wstring wstr = L"";

int position = 0;

_wfopen_s(&filePointer, fileToReadFrom, L"r");
_setmode(_fileno(filePointer), _O_U8TEXT);

wifstream file(filePointer);

getline(file, inputString);
while (inputString[position] != L' ')
{
    position++;
}
fseek(filePointer, position, SEEK_SET);//start reading after first word

while (file.good())
{
     getline(file, inputString);

     for (wsregex_iterator it(inputString.begin(), inputString.end(), biRegx), it_end; it != it_end; ++it)
     {
         //Filling the bigram container
         wstr = (wstring) (*it)[0];
         bigramStatMap[wstr]++;

     }
}

最佳答案

使用for循环,读取所需的单词数:

unsigned int words_before_begin;
std::string sux_string;
std::ifstream file_stream("Your file");

for(unsigned int i = 0 ; i < words_before_begin ; ++i)
    file_stream >> aux_string; //istream reads strings word by word, using spaces as separators.

/* Your reading code starts here */

关于c++ - 我如何指定要开始从C++文件中读取超过特定数量的单词?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17991241/

10-11 18:49