我遇到问题。当我尝试将文件加载到字符串数组中时,什么也没有显示。
首先,我有一个文件,在一行上有一个用户名,第二个有密码。
我还没有完成代码,但是当我尝试显示数组中什么都没有显示。
我很喜欢这个工作。C++ String Array,从文件加载文本行
有什么建议吗?
users.txt
user1
password
user2
password
user3
password
C++代码
void loadusers()
{
string t;
string line;
int lineCount=0;
int lineCount2=0;
int lineCount3=0;
ifstream d_file("data/users.txt");
while(getline(d_file, t, '\n'))
++lineCount;
cout << "The number of lines in the file is " << lineCount << endl;
string users[lineCount];
while (lineCount2!=lineCount)
{
getline(d_file,line);
users[lineCount2] = line;
lineCount2++;
}
while (lineCount3!=lineCount)
{
cout << lineCount3 << " " << users[lineCount3] << endl;
lineCount3++;
}
d_file.close();
}
谢谢大家的输入!希望我会用这些新获得的知识来解决我的问题。这需要我花几天的时间来弄清楚如何解决它。 – Luke 2010-10-10 20:31:36