将文件保存到目录C

Saving A File to Directory C++

本文关键字:文件 保存      更新时间:2023-10-16

所以我在这上花费了大约30分钟的时间,没有运气。尝试了许多保存文件的方法。当我将其保存到:
中时,它有效C: USER JSMIT ONEDRIVE Documents Visual Studio 2017 Projects Projects Password Generator Password Generator

,但是当我尝试将其保存到:

中时,

c: users jsmit oneedrive documents

或:

c: users jsmit documents new文件夹

这是我保存文件的代码:

void savePassword(string stringpassword, string site) {
ofstream out("C:DocumentsNew folderoutput.txt", ofstream::app); // DOESN'T WORK
out << site << ": " << stringpassword << endl; // This is where it saves the password into the text file
out.close(); // Closes file
}

如果我说:

ofstream out("Password.txt", ofstream::app); // ofstream:app stops overwrite

它有效。

edit ::::允许我保存到h: new文件夹,而不是c:drive?如何修复?

我如何制作它,以将其保存到:c: users jsmit oneDrive documents

问题是字符 使用 \/

有关更多详细信息,请参见此处:

在C中,所有逃生序列由两个或多个字符组成, 首先是后斜线,;其余字符确定 逃生序列的解释。例如, n是一个 逃脱序列表示新线字符。其余的 本文侧重于C;其他编程语言可能会 具有不同的语法和语义。

像其他人所说的一样,是逃生角色。您需要在包含的含义时使用双反斜线。

您不能只写入C:除非您是管理员。

使用"%userProfile%"的" home"文件夹的快捷方式访问此文件夹,然后可以使用"%userProfile% oneDrive documents"

另外,在将任何文件写入其中之前,请确保文件夹存在。该文件夹不会自动创建,您必须自己制作。

另外,看看其他答案,''字符应该是''