Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Access

Status
Not open for further replies.

pmorones

IS-IT--Management
Mar 15, 2002
17
0
0
US
I have a program that I have written to access a particular file. The program will open the file and read from it. My problem is that I do not know where to place the file so that my program can find it. I tried putting it in the source folder in the project. That doesn't work. I know it is a very simple question but can anyone help me out please.
 
1. Place it anywhere and use a full path:
Code:
fstream f("D:\\MyDir\\MyFile.xxx");
2. Use program parameter to point to the file:
Code:
int main(int argc, char* argv[])
{
   fstream f;
...
   if (argc < 2)
   {
      cout << "*** No file parameter" << endl;
      return 1;
   }
   if (f.open(argv[1]))
   {
      ...
3. In VC++ 6.0 IDE call place the file in project dir (it's workdir in that case) and use a simple name to open (worst solution):
Code:
ifstream f("MyFile.xxx"); // in default dir
// (different when start exe by click or in console window).

It's one of design tasks: you must set application files access specification before coding...
 
How does this work if I have the user enter a file name to search for? I want the user to enter a filename and my program to search for the existence of that file. Here is what I have so far.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main()
{

ifstream infile;
ifstream outfile;




string s;
cout << "Enter file name ";

cin >> s;

infile.open(s.c_str());
if (!infile){
cerr << "unable to open file ";
exit(0);
}
else

cout << s << '\n';

infile.close();
outfile.close();
return 0;

Even if I place the file mine.dat in the project, I always get "unable to open file" even when I enter mine.dat as the file to search for.
 
Works fine for me. What compiler are you using? I'm using VS.NET 2003 and putting it in the project directory, not the debug/release directory where the exe is. Are you doing the same?
 
I am using 6.0. I will give your suggestion a try and see what happens.
 
That worked. Thanks a lot! I appreciate the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top