CD not working...

General technological topics without their own forum go here

CD not working...

Post by xTractatorix on Sat Jul 14, 2012 4:23 pm
([msg=68057]see CD not working...[/msg])

Okay so im Windows 7 as my OS and C as my language.
Im trying to use the system() function to access and use the cmd. The problem though is that im trying to change directories,but its not allowing me. Im typing
Code: Select all
system("cd C:\\)
but its not working. Anyone have any ideas why not? Also it says in the command line when i execute the code "Volume in drive C has no label".
xTractatorix
Experienced User
Experienced User
 
Posts: 61
Joined: Sun May 13, 2012 8:42 am
Blog: View Blog (0)


Re: CD not working...

Post by Acidiferous on Sat Jul 14, 2012 4:40 pm
([msg=68058]see Re: CD not working...[/msg])

A little background on what you are trying to do would help, or maybe it's just me who is slow :)

Anyway, if you are planning on making 2 commands in a row, where one should start where the last one stopped, it's not going to work like that.
I dont know if it's the forum and the pasting that ate a little of your code, but i spottet a minor error, you are missing a "

Descripe what you are trying to do and i will be happy to help you, if i can :)
Acidiferous
Experienced User
Experienced User
 
Posts: 61
Joined: Tue Mar 29, 2011 9:49 am
Location: Europe
Blog: View Blog (0)


Re: CD not working...

Post by xTractatorix on Sat Jul 14, 2012 5:41 pm
([msg=68060]see Re: CD not working...[/msg])

Acidiferous wrote:A little background on what you are trying to do would help, or maybe it's just me who is slow :)<br><br>Anyway, if you are planning on making 2 commands in a row, where one should start where the last one stopped, it's not going to work like that.<br>I dont know if it's the forum and the pasting that ate a little of your code, but i spottet a minor error, you are missing a "<br><br>Descripe what you are trying to do and i will be happy to help you, if i can :)

Yeah sorry about the vaugness of my post i was in a hurry. Okay what im trying to do is use the system() function to automate commands in my C program. What im having a problem with is not build errors its that when i try to change directorys usind the cd command its gives me the message in the command line "Volume C has no label". I dont know what im doing wrong.. heres my sorce code.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
  system ("cd C:\\");
  return 0;
}
xTractatorix
Experienced User
Experienced User
 
Posts: 61
Joined: Sun May 13, 2012 8:42 am
Blog: View Blog (0)


Re: CD not working...

Post by centip3de on Sat Jul 14, 2012 7:22 pm
([msg=68063]see Re: CD not working...[/msg])

xTractatorix wrote:Yeah sorry about the vaugness of my post i was in a hurry. Okay what im trying to do is use the system() function to automate commands in my C program. What im having a problem with is not build errors its that when i try to change directorys usind the cd command its gives me the message in the command line "Volume C has no label". I dont know what im doing wrong.. heres my sorce code.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
  system ("cd C:\\");
  return 0;
}


From what I understand, though I could be wrong, you can't change the current directory of an executable. If you want to do this in an interpreted language, go for it. However, if you want to remain in a compiled language, you cannot change the directory. You can, however, open a new process and set it's working directory to the one you're looking for.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1428
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: CD not working...

Post by WallShadow on Sat Jul 14, 2012 7:35 pm
([msg=68064]see Re: CD not working...[/msg])

I'm not completely certain as to what (if anything) this actually does. As I see it, by calling the "cd C:\\" process, you are creating a new process with it's own execution environment, and the cd command only changes the new process's execution environment variables, not the environment of you c program. In other words, your starting a new process, changing that process's directory to C:\, and then the process kills itself (cause it has nothing else to do, cd is over).
User avatar
WallShadow
Contributor
Contributor
 
Posts: 594
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: CD not working...

Post by mShred on Sun Jul 15, 2012 2:44 am
([msg=68068]see Re: CD not working...[/msg])

Everything above said makes sense. But from what I understand, when you run the system() function, it'll execute your command. Then once that command is executed, it'll return back to your process having completed absolutely nothing. Like cent said, you aren't in a terminal so you can't change directories. If you were going to do something like copying a file, you would probably just want to run the copying function in C while expressing the file's completely path. "C:\windows\system32\mypenisyourface.txt", rather than cd'ing to \system32\ and copying the file.
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1742
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: CD not working...

Post by pertti123 on Sun Jul 15, 2012 10:59 am
([msg=68072]see Re: CD not working...[/msg])

I dont' get any output running your program. This runs just fine and prints the contents of the root directory.

Code: Select all
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    if (system("cd c:\\ && dir") != 0) {
        perror("system");
        return 1;
    }

    return 0;
}

Or you can change the current working directory of the running process.

Code: Select all
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    if (chdir("..") != 0) {
        perror("chdir");
        return 1;
    }
    if (system("dir") != 0) {
        perror("system");
        return 1;
    }

    return 0;
}
pertti123
New User
New User
 
Posts: 13
Joined: Fri Sep 10, 2010 6:02 am
Blog: View Blog (0)


Re: CD not working...

Post by WallShadow on Sun Jul 15, 2012 11:07 am
([msg=68073]see Re: CD not working...[/msg])

pertti123 wrote:
Code: Select all
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    if (chdir("..") != 0) {
        perror("chdir");
        return 1;
    }
    return 0;
}



That'll come in handy some day.

Hmm, just dug up this:

http://msdn.microsoft.com/en-us/library ... 80%29.aspx
http://msdn.microsoft.com/en-us/library ... 80%29.aspx

according to microsoft, chdir() is unsafe and that we should use My. Truth, or conspiracy?
User avatar
WallShadow
Contributor
Contributor
 
Posts: 594
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)



Return to General

Who is online

Users browsing this forum: No registered users and 0 guests