英文:
Ofstream does create but doesn't write to file
问题
以下是代码的翻译:
void makefiles(char* filename) {
ifstream file(filename, ios::in);
// 主文件中的条目数
int size = 0;
// names 是保存条目的向量
vector<string> names;
string tmpstr;
while (getline(file, tmpstr)) {
string toadd = "";
for (int i = 0; i < tmpstr.size(); i++) {
if (tmpstr[i] != '|')
toadd += tmpstr[i];
else
break;
}
if(count(names.begin(), names.end(), toadd) == 0)
names.push_back(toadd);
size++;
}
file.close();
ofstream userfile;
file.open(filename, ios::in);
for (int i = 0; i < names.size(); i++) {
userfile.open(string(getenv("TEMP")) + "\\" + names[i] + ".txt", ios::out);
cout << string(getenv("TEMP")) + "\\" + names[i] + ".txt" << endl;
while (getline(file, tmpstr)) {
if (tmpstr.rfind(names[i], 0) == 0) {
userfile << tmpstr << endl;
userfile.flush();
}
}
userfile.close();
}
file.close();
}
该函数从主函数中调用,filename 包含一些格式化的文本,例如下面的电子邮件数据库视图:
gottaletitout@gmail.com|timetogo@gmail.com|06.01.2023|21:09:30|born in the abyss|156
gottaletitout@gmail.com|timetogo@gmail.com|06.01.2023|21:09:30|born in the abyss|156
a@gmail.com|b@gmail.com|23.12.2009|13:52:16|prikol|13
hottaletitout@gmail.com|timetogo@gmail.com|06.01.2023|21:09:30|aorn in the abyss|156
因此,每一行都是一个条目。
总结一下,我得到了许多空文件,它们的名称是我想要的,但是它们是空的。
我尝试在打开 userfile 后面添加以下代码:
if(!userfile){
cout << "文件未打开" << endl;
return;
}
但是它没有帮助,因为 !userfile 的值为 false。
我尝试使用 userfile << tmpstr << endl 替换为 userfile << tmpstr << flush;
英文:
Here is the code:
void makefiles(char* filename) {
ifstream file(filename, ios::in);
// number of entries in main file
int size = 0;
// names is the vector that holds entries
vector<string> names;
string tmpstr;
while (getline(file, tmpstr)) {
string toadd = "";
for (int i = 0; i < tmpstr.size(); i++) {
if (tmpstr[i] != '|')
toadd += tmpstr[i];
else
break;
}
if(count(names.begin(), names.end(), toadd) == 0)
names.push_back(toadd);
size++;
}
file.close();
ofstream userfile;
file.open(filename, ios::in);
for (int i = 0; i < names.size(); i++) {
userfile.open(string(getenv("TEMP")) + "\\" + names[i] + ".txt", ios::out);
cout << string(getenv("TEMP")) + "\\" + names[i] + ".txt" << endl;
while (getline(file, tmpstr)) {
if (tmpstr.rfind(names[i], 0) == 0) {
userfile << tmpstr << endl;
userfile.flush();
}
}
userfile.close();
}
file.close();
}
This function is being called from main, filename contains some formatted text, like email database of the next view:
gottaletitout@gmail.com|timetogo@gmail.com|06.01.2023|21:09:30|born in the abyss|156
gottaletitout@gmail.com|timetogo@gmail.com|06.01.2023|21:09:30|born in the abyss|156
a@gmail.com|b@gmail.com|23.12.2009|13:52:16|prikol|13
hottaletitout@gmail.com|timetogo@gmail.com|06.01.2023|21:09:30|aorn in the abyss|156
Thus, one line is one entry
In conclusion, I get many empty files that have the names i want, but, again, they are empty
I tried adding right after opening userfile:
if(!userfile){
cout << "File is not opened" << endl;
return;
}
But it doesn't help because !userfile = false
I tried instead of userfile << tmpstr << endl, using userfile << tmpstr << flush;
答案1
得分: 1
你只写了一个文件。在最后一个for
循环的第一次迭代中,当i==0
时,内部的while
循环将file
读取到文件末尾(EOF)。在所有后续的迭代中,file
已经到达EOF,所有的getline
调用都会立即失败。因此,除了第一个文件(以names[0]
命名的文件)之外,其他所有文件都被创建但没有被写入。
英文:
You are writing at most one file. On the first iteration of the last for
loop, with i==0
, the inner while
loop reads file
all the way to EOF. On all subsequent iterations, file
is already at EOF and all getline
calls fail immediately. So all the files but the first (the one named after names[0]
) are created but never written to.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论