Jump to content

C++ system() help


lukeh53
 Share

Recommended Posts

Okay, i am making a program and i need to make files in a certain directory, so i figured i would use the mkdir that is already in the command prompt. my problem is that when i pass C: through system() it doesnt change the directory. cd wont do anything either.

thanks in advance,

-luk53

Link to comment
Share on other sites

  • 1 month later...

You don't need the system command for that, just use the directory's path.

For instance, if you wanted to make a text file on the C: drive you would do something like:

fout.open ( "C:\mytext.txt" )

That will make a text file in that path (kinda straight forward :D )

But if you don't want it so exact, use something more like:

fout.open ( "..\mytext.txt" )

That will make the file in the directory above where your program is.

I hope that's what you needed.

Link to comment
Share on other sites

  • 2 months later...

When you write file names like C:\Bla\bla.txt C++ sees it as C:Blabla.txt. This is because \ is reserved for escape sequences. So you need to use the escape sequence to make the \. So it should be C:\\Bla\\bla.txt

Link to comment
Share on other sites

Okay, i am making a program and i need to make files in a certain directory, so i figured i would use the mkdir that is already in the command prompt. my problem is that when i pass C: through system() it doesnt change the directory. cd wont do anything either.

thanks in advance,

-luk53

There is no need to use the cd command, you can pass an absolute path to the command which will indicate exactly where it should be creating the new folder, like this:

system("mkdir C:\\parent_folder\\new_folder");

You should probably try out requiem's method however, as if I am correct the method he has showed you will automatically create the directory if it doesn't exist when the file is created :) A lot less messy then messing around with system commands.

Edited by etftw
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...