user1140755
user1140755

Reputation: 45

How to read file from local directory path

I have one function which reads file and does the conversion part.

fp=fopen("newfile.txt","r");

Here i have copied this newfile.txt in project file and compiling in VC++ 2008 IDE.It works fine

I would like to read the file from a local drive directory path.is it possible to read the files from local drive.how to mention the path.If so please mention any example.

one more thing If i want to read all the files in that particular folder with out changing the name of text files in the above code. Suggest me any thing to do.

I dont want to change the file name manully in the code

Upvotes: 0

Views: 32932

Answers (2)

Daniikk1012
Daniikk1012

Reputation: 13

I think you can use first program argument. It is a string containing path of executable. You can access it by usingint main(int argc, char *args[]) instead of int main(). The args[0] contains what you need. Just take a substring of it, to get the path and concatenate it with your filename.

Upvotes: 0

Cyclonecode
Cyclonecode

Reputation: 30021

You could use an absolute path to your file:

FILE* fp = fopen("c:\\your_dir\\your_file.txt", "r");
if(fp) {
   // do something
   fclose(fp);
}

or a relative path, assuming your file is located in c:/etc and your executable is located in c:/etc/executables:

FILE* fp = fopen("..\\your_file.txt", "r");
if(fp) {
   // do something
   fclose(fp);
}

Upvotes: 3

Related Questions