C ++代码获取文件行并读取该行的第二个单词?

时间:2009-05-30 02:04:22

标签: c++

#include <iostream>
#include <string>
#include <fstream>

using namespace std ;


string strWord( int index , string line)
{
       int count = 0;
       string word;
       for ( int i = 0 ; i < line.length(); i++)
       {
           if ( line[i] == ' ' )
           {
                if ( line [i+1] != ' ')
                {

                    count ++;
                    if (  count == index)
                    {
                       return word;
                    }
                    word ="";
                }
           }
           else
           {
                word += line[i];
           }       
       }
}







int main ( )
{
    ifstream inFile ;
    inFile.open("new.txt");
    string line  , id;

    cout <<"Enter id : ";
    cin >>id;
    while(!inFile.eof() )
    {
                        getline ( inFile , line );
                        if ( strWord ( 1, line ) == id ) 
                        {
                             cout <<strWord ( 2 , line ) <<endl;
                             break;
                        }
    } 
    system("pause");
}

问题是:有人可以向我解释这个我不知道它在做什么我的意思是我得到了这个概念,但每条线路在做什么?

5 个答案:

答案 0 :(得分:3)

您想要每行的评论

// function that returns a word from 'line' with position 'index'
// note that this is not a zero based index, first word is 1,
// second is 2 etc ..
string strWord(int index, string line)
{
    int count = 0; // number of read words
    string word; // the resulting word
    for (int i = 0 ; i < line.length(); i++) { // iterate over all characters in 'line'
        if (line[i] == ' ') { // if this character is a space we might be done reading a word from 'line'
            if (line[i+1] != ' ') { // next character is not a space, so we are done reading a word
                count++; // increase number of read words
                if (count == index) { // was this the word we were looking for?
                    return word; // yes it was, so return it
                }
                word =""; // nope it wasn't .. so reset word and start over with the next one in 'line'
            }
        }
        else { // not a space .. so append the character to 'word'
           word += line[i];
        }       
    }
}


int main( ) // main function of the program, execution starts here
{
    ifstream inFile; // construct input file stream object
    inFile.open("new.txt"); // associate the stream with file named "new.txt"
    string line, id; // 

    cout << "Enter id : "; // write "Enter id :" to console
    cin >> id; // read input from console and put the result in 'id'

    while (!inFile.eof()) { // do the following as long as there is something to read from the file
        getline(inFile, line); // read a line from the file and put the value into 'line'
    if (strWord(1, line) == id) { // if the first word in 'line' equals 'id' ..
        cout << strWord(2, line) << endl; // prints the second word in 'line'
        break; // exits the while loop
    }
    } 

    system("pause"); // pause the program (should be avoided)
}

答案 1 :(得分:2)

我没有仔细阅读,但看起来它打印出第一个单词是用户输入的第二个单词。

答案 2 :(得分:2)

也许这会有所帮助:

// return word[index] from line
string strWord( int index , string line) // word index and actual line
{
  int count = 0; // current word index
  string word; // word buffer (return value)

  for ( int i = 0 ; i < line.length(); i++) // loop through each character in line
  {
    if ( line[i] == ' ' ) // if current character is a space
    {
      if ( line [i+1] != ' ')  // but next char is not space, we got a word
      {
        count ++; // incr current word index counter in line
        if (  count == index) // if count == looked for index return word
        {
          return word;
        }
        word ="";  // otherwise reset word buffer
      }
    }
    else // character is not space
    {
       word += line[i]; // add letter/digit to word buffer
    }       
  }
}

例如,当行以空格开头时,上面的算法失败,所以这里有许多关于需要处理的数据内的假设,如果索引无效或者内联为空则不需要处理。如果函数失败,也会丢失最后一个返回值。

答案 3 :(得分:1)

你有一个C ++的交互式调试器吗?例如,如果将此代码加载到Microsoft Visual C ++中,则可以一次单步执行一个语句,随时检查每个变量的值。这是了解细节级别的新代码的好方法。

答案 4 :(得分:0)

有一个名为strWord的函数可以从句子中获取任何索引并返回它。

主程序打开一个包含id和每行中另一个单词的文件,并准备好从中读取。

然后它询问用户的id,检查id是否是文件中任何行中的第一个单词。如果是,则从该行获取第二个单词并打印出来。

这就是全部。询问您是否遇到特定行或一堆行的问题。

或者正如格雷格建议的那样,去找一个调试器。这将是理解任何逻辑的最简单方法。