从文件中读取字符串到数组中

Posted

技术标签:

【中文标题】从文件中读取字符串到数组中【英文标题】:Reading strings from file into array 【发布时间】:2011-04-26 16:32:15 【问题描述】:

嘿。我正在尝试将字符串从包含单词列表的文件中读取到数组中。这样我就可以通过查看字符串是否存在于我的数组中来检查它们是否是真实的单词。除了比较之外,我一切正常。我的二进制搜索甚至通过了有问题的单词。当它比较两个完全相同的单词时,它仍然返回false。我认为问题可能出在我输入单词的方式上,因为 string.compare() 函数可以正常工作。这是那个代码。我很想得到一些帮助。谢谢。

  ifstream dictFile;
  dictFile.open("dictionary.txt");
  if (!dictFile) // testing if file open
    
      cout << "Error opening dictionary file" << endl;
    
  int index = 0; // dictionary must progress start at line 1
  while(!dictFile.eof())
    
      getline(dictFile,dictionary[index]);
      index++;
    
  dictFile.close();

我这样做有什么明显的错误吗?

编辑 这里也是对比代码

bool database::is_word(string word)

  int ii;
  int comp;
  int min = 0;
  int max = dictSize;
  // this will go into the dictionary and look for the word
  // it uses a binary search pattern
while (min<=max)
    
      ii = (min+max)/2;
      comp = word.compare(dictionary[ii]);
      cout <<dictionary[ii];
      if (comp==0)
    
      cout << word<< " is a word!" << endl;
      return 1;
    
      else if (comp < 0)
    
      max = ii-1;
    
      else
    
      min = ii+1;
      
      
 cout << word << " is NOT a word!" << endl;
  return 0;

【问题讨论】:

getline 是否在检索单词以及行尾的 \n(返回)?如果是这样,比较可能会认为这些词是不同的,因为它看起来像“word”!=“word\n”。只是一个想法。 @Tyler getline() 删除换行符。 @unapersson 好的,很高兴知道。谢谢。 您可以添加用于字符串比较的代码吗?这可能是您调用比较的任何函数的问题,而不是文件读取问题本身。 嗯...你怎么称呼is_word 并且你确定你的dictSize 变量是有效的?我刚刚写了一些快速测试代码,它非常适合我。 【参考方案1】:

又不是 eof() 函数!你想要:

while( getline(dictFile,dictionary[index]) ) 
  index++;

(假设 dictionary 是明智的,但可能不是)因为 eof() 无法预测下一次读取是否有效。

哦,人们是从哪里开始使用 eof() 的呢?这就像一种疾病!

【讨论】:

在某处看到它...我会立即记下不再使用它。但是,此修复程序会产生相同的错误。 我更喜欢这样写:for ( string line; getline(input,line); ) ... ,因为它更惯用,允许对行内容进行后处理,并且跨容器类型是统一的。 @Andre 我不能说我认为它是惯用的 - 我希望 for 循环从一个已知值循环到另一个,而不是不确定的。【参考方案2】:

如果我的目标是简洁而不是性能,这就是我执行整个程序的方式。

// read the dictionary 

vector<string> dictionary;

  ifstream dictionary_file("dictionary.txt");
  istream_iterator<string> begin(dictionary_file);
  istream_iterator<string> end;
  while( begin != end )
    dictionary.push_back( *begin++ );
  sort( dictionary.begin(), dictionary.end() );


// read the input file and test against the dictionary


  ifstream input_file("input.txt");
  istream_iterator<string> begin(input_file);
  istream_iterator<string> end;
  while( begin != end )
  
    string input = *begin++;
    vector<string>::iterator it = lower_bound( dictionary.begin(), dictionary.end(), input );
    if( it != dictionary.end() && *it == input )
      cout << input << " found!" << endl;
    else
      cout << input << " not found!" << endl;
  

【讨论】:

使用std::set 可能会给你更好的性能,使意图更清晰,并简化它:dictionary.find(word) != dictionary.end() 比在矢量上使用lower_bound() 更清晰! 你是对的,std::set 会读得更清楚,但你错了它会有更好的性能。 std::set 和排序的 std::vector 在搜索项目时应该具有完全相同的性能。

以上是关于从文件中读取字符串到数组中的主要内容,如果未能解决你的问题,请参考以下文章

c语言中从文件中按行读取字符串,并进行相应操作。

从文件中读取字符串并使用 Groovy 将它们放入数组中

如何将数据库的数据读取到数组中

c语言读取文件然后保存到数组中

字符输入流读取文本文件ReaderFileReaderBufferedReader

字符输入流读取文本文件ReaderFileReaderBufferedReader