【发布时间】:2015-03-08 19:14:35
【问题描述】:
我有一个这种形式的文本文件:
1 1
2 2
3 3
#
4 3
5 1
现在我想读取这个文本文件并计算两个变量num1 和num2。 num1 计算# 之前的所有字符数,num2 计算# 之后的所有字符数。
到目前为止我的代码:
Graph::Graph(string s) // s is the filename
{
ifstream tgf(s);
string line;
// num1 and num2 are two private member (type int) of class Graph
num1 = 0;
num2 = 0;
if(tgf.is_open())
{
while(getline(tgf, line, '#')) // this should read tgf until '#'
{
num1++;
}
} else
{
cout << "Can´t open textfile!" << endl;
}
}
我不知道如何为我的问题编写代码。
【问题讨论】:
标签: c++ string fstream getline stringstream