How to read a file?

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

  • Answer:

    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); }

user1140755 at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.