Ofstream创建但不写入文件。

huangapple go评论68阅读模式
英文:

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&lt;string&gt; names;
	string tmpstr;

	while (getline(file, tmpstr)) {
		string toadd = &quot;&quot;;
		for (int i = 0; i &lt; tmpstr.size(); i++) {
			if (tmpstr[i] != &#39;|&#39;)
				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 &lt; names.size(); i++) {
		userfile.open(string(getenv(&quot;TEMP&quot;)) + &quot;\\&quot; + names[i] + &quot;.txt&quot;, ios::out);
		
		cout &lt;&lt; string(getenv(&quot;TEMP&quot;)) + &quot;\\&quot; + names[i] + &quot;.txt&quot; &lt;&lt; endl;
		
		while (getline(file, tmpstr)) {
			if (tmpstr.rfind(names[i], 0) == 0) {
				userfile &lt;&lt; tmpstr &lt;&lt; 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 &lt;&lt; &quot;File is not opened&quot; &lt;&lt; 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.

huangapple
  • 本文由 发表于 2023年1月8日 22:42:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定