我正在写一个练习的程序,它将读取文件中的数据并将其格式化为可读。到目前为止,我有一些代码会将头部与其下的数据分开。那就是:为什么不执行for循环?
int main() {
ifstream in("records.txt");
ofstream out("formatted_records.txt");
vector<string> temp;
vector<string> headers;
for (int i = 0; getline(in,temp[i]); ++i) {
static int k = -1;
if (str_isalpha(temp[i])) {
headers[++k] = temp[i];
temp.erase(temp.begin() + i);
}
else {
temp[i] += "," + headers[k];
}
}
}
(str_isalpha()
只是适用isalpha()
在一个字符串中的每个字符的功能)现在,for循环在这个程序不执行,我想不通为什么。有人知道吗?
编辑:至于建议,我把它改成
string line;
for (int i = 0; getline(in,line); ++i) {
temp.push_back(line);
不过跳过for循环干脆。
是getline参数中的逗号吗? – Holograham 2010-05-02 01:22:13
是的,这就是参数如何在函数中分离,不是吗? – Maulrus 2010-05-02 01:34:10