1

So i've spent about 30minutes on this with no luck. Tried many ways of saving the file. It works when i save it into:
C:\Users\jsmit\OneDrive\Documents\Visual Studio 2017\Projects\Password Generator\Password Generator

but not when i try and save it into:

C:\Users\jsmit\OneDrive\Documents

or:

C:\Users\jsmit\Documents\New folder

This is my code for saving a file:

void savePassword(string stringpassword, string site) {

ofstream out("C:\Documents\New folder\output.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

}

If i put:

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

it works.

EDIT:::: Allows me to save to H:\New folder but not C: drive? How to fix?

How do i make it so it saves it into: C:\Users\jsmit\OneDrive\Documents

IVIaximumPower
  • 131
  • 1
  • 3
  • 9
  • 5
    Either use forward slashes, or escape the backslashes with an extra backslash. –  Feb 21 '17 at 14:00
  • 3
    Basically: \ -> \\ ([Printing the path](http://coliru.stacked-crooked.com/a/3ff223f074e3ab69) makes it obvious what the problem is, you even get compiler warnings!) – Borgleader Feb 21 '17 at 14:00
  • 4
    You need to escape the backslashes: `"C:\\Documents\\New folder\\output.txt"` – πάντα ῥεῖ Feb 21 '17 at 14:00
  • 1
    @πάνταῥεῖ Tried this didnt work for some reason – IVIaximumPower Feb 21 '17 at 14:02
  • @Borgleader Nope, i get no errors. – IVIaximumPower Feb 21 '17 at 14:02
  • @IVIaximumPower "I get errors", what errors? – Borgleader Feb 21 '17 at 14:05
  • 1
    Are you sure the destination folder exists and your process has the write access to it, so it is allowed to create and/or overwrite the destination file? – CiaPan Feb 21 '17 at 14:05
  • @CiaPan Not sure, how do i check? It is able to write into the projects folder where the solution is made from. – IVIaximumPower Feb 21 '17 at 14:07
  • Make sure the folder you are trying to write the file to exists. It will not be created in the process. – drescherjm Feb 21 '17 at 14:11
  • Hopefully [When will ofstream::open fail?](http://stackoverflow.com/q/5835848/733637) or [Error handling in std::ofstream while writing data](http://stackoverflow.com/q/28342660/733637) contain useful answers. Or any of [Search for ofstream+fail](http://stackoverflow.com/search?q=ofstream+fail)... – CiaPan Feb 21 '17 at 14:11
  • @drescherjm The file does exist. I've done it so there is no txt file in the folder and there is a txt file called "output.txt" and both dont work. – IVIaximumPower Feb 21 '17 at 14:14
  • @everyone So i'm able to save to my H drive put not my C: Drive? Im running VS as admin and am a Windows Admin??? – IVIaximumPower Feb 21 '17 at 14:24
  • Parts of your C: drive are protected by UAC. – drescherjm Feb 21 '17 at 14:26
  • @drescherjm even my documents? If i disable UAC will that fix it? – IVIaximumPower Feb 21 '17 at 14:27
  • No but you seem be using a different path. I mean `C:\\Documents\\New folder` is not your profile. With that said Admin may not have rights on your user profile path. – drescherjm Feb 21 '17 at 14:27
  • Admin may not have rights on your user profile path. Run visual studio as a normal user. And use the correct path with \\ instead of \ for the path separator or just use / – drescherjm Feb 21 '17 at 14:29
  • @drescherjm Worked to allow me to write to C:\\Users\\jsmit\\Documents\\New folder\\output.txt but not OneDrive? – IVIaximumPower Feb 21 '17 at 14:35
  • I have no experience with OneDrive. I just disable it since I already have have an unlimited cloud drive from a different provider. – drescherjm Feb 21 '17 at 14:37
  • @drescherjm Got it to save to OneDrive! Thanks for everyones help. Still no clue why it didnt work before. I just restarted my PC and seemed to fix it. – IVIaximumPower Feb 21 '17 at 14:38

3 Answers3

4

The problem is the character \ use \\ or /

See here for more details:

In C, all escape sequences consist of two or more characters, the first of which is the backslash, \; the remaining characters determine the interpretation of the escape sequence. For example, \n is an escape sequence that denotes a newline character. The remainder of this article focuses on C; other programming languages are likely to have different syntax and semantics.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
0

You can't just write into C: unless you are admin.

Use the shortcut for the Home Folder "%USERPROFILE%" to access this folder, then you can use "%USERPROFILE%\OneDrive\Documents"

Also, make sure the folder exists before writing any file into it. The folder won't be automatically created, you have to make it yourself.

Also, take a look at other answers, '\' character should be '\\'

0

Like others said \ is the escape character. You need to use double backslashes when meaning for \ to be included.

James F
  • 169
  • 10