我正在为一个练习编写一个程序,该程序将从文件中读取数据并将其格式化为可读。到目前为止,我有一些代码可以将标题与下面的数据分开。这里是:
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 循环。