#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");
}
問題是:有人可以向我解釋這我不明白它在做什麼我的意思是我得到的概念,但是每行是做什麼?C++代碼獲取文件行並讀取行的第二個單詞?
請修正你的代碼的註釋每行前使用4個空格。這是功課嗎? – SingleNegationElimination 2009-05-30 02:09:31
修復了格式。 @ H4cKL0rD,請記住在通過縮進四個空格將其粘貼到Stack Overflow時對其進行格式化。 – 2009-05-30 02:24:59