Consider a line of simple C code:

fopen("C:\\project\\test1.dat","r");
it "directs" the program to the directory where it can find the file from which it will read.

My question is under what circumstances, I may write
fopen("test1.dat","r") instead?

Does the program need to be in the same folder / directory as test1.dat?

Thanks

Consider a line of simple C code:

fopen("C:\\project\\test1.dat","r");
it "directs" the program to the directory where it can find the file from which it will read.

My question is under what circumstances, I may write
fopen("test1.dat","r") instead?

Does the program need to be in the same folder / directory as test1.dat?

Thanks

If you use fopen("test1.dat","r") which is the typical usage (no path information given), fopen will look in the current working directory. This the directory where your project file (.dsw in case of VC++ 6.0 and .sln in case of VC++ 7.1) is present. If u run your programe from explorer, say by clicking on it, current woring directry is one in which .exe of your application is present.

In addition, if the file is NOT one the current working directory the program may have to change the current working directory to where the file is. There are a few operating systems where there is no concept of "current working directory", in which case the program has no choice but to specify the full path.

thanks dragon

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.