Hi.
How would I go about locating a file on the computer, and using it in my application. For example, say I wanted to call up a text document for a help menu to aid the user. I would also like to know how you might do this even if the text file is anywhere on the computer...

If this isn't possible without heaps of coding, how would I call up a file from a certain location. Would I use the system() function?


Thanks guys.

do you mean this

//refered to file in current directory
	string MyFile="MyFile.txt";

	//for entring user path

//	cout<<"Please enter the file location and name : ";
//	cin>>MyFile;

	//create stream oject to maniulate with file
	ifstream MyReader;

	//open the file in input mode and binary formate
	MyReader.open(MyFile.c_str(),ios::in|ios::binary);

	
	//for opening error
	if(!MyReader.is_open())
	{
        cerr<<".....File Open Failed !!!!!"<<endl;
		exit(1);
	}

The ofstream class is used to open a file. While opening a file we generally provide only the name of the file, so the program searches for that name in the same folder.

You can also provide the location of the file so that the program opens that particular file

char file[20];
strcpy(file,"C:\\Myfolder\\filename");           // You can also input the location of the file
Ofstream outfile;
outfile.open(file);

Hope this helps.

What krnekhelesh said, except it's ifstream since use implies read, not write. To answer the rest of the question, searching your entire disk for the file is
1) difficult to program
2) time consuming to run

Best to just read the file from a specific location.

> How would I go about locating a file on the computer, and using it in my application?
no really portable way of doing it (if you want a fast search).
you could do this on unix system( "/usr/bin/locate 'file_name_glob' > files_found.txt" ) ; and then pickup the complete path(s) from files_found.txt. this assumes that the locate database is recomputed periodically. /usr/bin/find would always work; but could be quite slow.
on windows, you could use the indexing service api.
on machines where google desktop search + sdk is installed, you could also use its api.

This article has been dead for over six months. Start a new discussion instead.