[英]C++ code to get line of file and read the second word of the line?
#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");
}
问题是:有人可以向我解释一下吗?我不明白我在做什么,我的意思是我明白了,但是每一行在做什么?
您想要每行的评论
// 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)
}
我没有仔细阅读,但是看起来它打印出该行的第二个单词,第一个单词是用户输入的单词。
也许这可以帮助:
// 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
}
}
}
例如,当行以空格开头时,上面的算法将失败,因此此处存在许多需要处理的有关数据中的假设,例如如果索引无效或如果行为空,则不进行任何处理。 如果函数失败,则还会丢失最后一个返回值。
您有用于C ++的交互式调试器吗? 例如,如果将此代码加载到Microsoft Visual C ++中,则可以一次浏览一个程序语句,同时检查每个变量的值。 这是学习新代码在细节级别上做什么的好方法。
有一个名为strWord的函数,用于从句子中获取带有任何索引的单词并将其返回。
主程序打开一个文件,在每行中包含一个ID和另一个单词,并准备将其读取。
然后,它向用户询问ID,检查该ID是否为文件中任何行中的第一个单词。 如果是,则从该行中提取第二个单词并打印出来。
就这样。 询问您对特定的线或线束是否有问题。
或如Greg建议的那样,去调试器。 这将是理解任何逻辑的最简单方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.