HI guys.
Im a student and studying c++ that and cmd and so far i stil havent figure out how to change my current dir while my program is running.


My program is that i want to make a test.txt file with a "my test file" inside it , in the drive c: directory

#include<stdio.h>
#include<stdlib.h>
void changedir()
{
//system("cd c:");  doesnt change my dir
//system("c: ");     doesnt change my dir


}
void main(){


//changedir();                                     //change dir that doesnt work.

system("echo my test file > test.txt");       //writes my test file inside the test.txt file.

//getche();
}

HI guys.
Im a student and studying c++ that and cmd and so far i stil havent figure out how to change my current dir while my program is running.

See _chdir() function

My program is that i want to make a test.txt file with a "my test file" inside it , in the drive c: directory

#include<stdio.h>
#include<stdlib.h>
void changedir()
{
//system("cd c:");  doesnt change my dir
//system("c: ");     doesnt change my dir


}
void main(){


//changedir();                                     //change dir that doesnt work.

system("echo my test file > test.txt");       //writes my test file inside the test.txt file.

//getche();
}

don't use the system() function for that. Just open a stream object and write to it.

FILE* fp = fopen("test.txt","w");
fprintf(fp,"my test file\n");
fclose(fp);

Your program does not have to be in the same directory where that file is located. You can also specify the full path to the file

FILE* fp = fopen("c:\\some directory\\test.txt","w");

FILE *fp=fopen("c:\\documents and settings\\test.txt","w");
How come i cant make file in Documents and Settings also in Program Files
But if ill just simple place
FILE *fp=fopen("c:\\test.txt","w");
it can write.

is my syntax correct in the document and settings??

BTW is there any tutortial about system("")? like i want to change dir open files using system("")

Your syntax is fine. Run this and check.

#include <stdio.h>

int main()
{
   FILE *fp=fopen("c:\\documents and settings\\test.txt","w");
   fputs("test", fp);
   fclose(fp);
   return 0;
}

BTW is there any tutortial about system("")? like i want to change dir open files using system("")

Probably not -- it is just too simple a function for any tutorial. All you can use it for is things that can be done in a command prompt window. You can NOT use it to change a directory in a C or C++ program.

The chdir() function only affects the working directory of the current process.

The system() function creates a new process in order to execute the command.

but is there another way to change directory or open executable files in a particular place using c?

Yes, I already told you how to do it 7 hours ago. use _chdir() function to change the working directory. There are several functions you can use to spawn another executable program. system() is one of them, CreateProcess() is another on MS-Windows operating system.

You can do this to run programs from a particular place:

system("c:\\folder\\prog.exe");

> FILE *fp=fopen("c:\\documents and settings\\test.txt","w");
Lemme guess, you're using Turbo C.
It's no surprise really, everyone seems to for all the wrong reasons.

One of these reasons being that it was written WAY (WAY) before long filenames, and spaces were allowed in filenames. This (and many other) compatibility horrors await you.

Do yourself a monster favour and get a compiler compatible with your OS. This means actually compatible, and not one that just happens to make "hello world" work.

Ok Then Ill try other function.

Yes i am using Turbo C..

Ok Then Ill try other function.

Yes i am using Turbo C..

Stop using that compiler. There are several free compilers you can get that work with long file names.

can you named me one..coz i dont know any other compiler... all i know is c language...
is there another compiler that is just same as c?

C++ is just same as c?

No it's not. But these compiler will be happy to compile your C code for you, as well as C++ code.

c++ compilers also compile pure C code -- its like getting two compilers for the price of one (which in this case is zero). :)

I don't use Borland Turbo C++ (and neither should you),

What's the problem with Borland Turbo C++?

Turbo doesn't comply with the C++ standard that was introduced in 1999 (the C99 standard). To be short: Turbo is outdated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.