0

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

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]++;

     }
}
4

2 回答 2

3

使用 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 */
于 2013-08-01T10:21:57.030 回答
1

复制整个向量

std::ifstream stream ("file_name");

std::vector<std::string> words;

    std::copy(std::istream_iterator<std::string>(stream),
                  std::istream_iterator<std::string>(),
                  std::back_inserter(words)
                 );

从开始访问words[position+1]

于 2013-08-01T10:38:18.737 回答