Opening File Failure

Hi:

I'm using wxDevC++ and Vista.

I'm having a problem with file opening. I have a file called testFile.txt in the same directory as the following simple program:

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
ifstream infile;
infile.open("testFile.txt");
if (infile.fail())
{
cout << "file opening failed" << endl;
}
system("PAUSE");
exit (1);
}

When I run the program, I get the "file opening failed" message. I've tried variations in the code for opening the file, and they all seem to fail, although the file is clearly in the directory. For example, this code (taken from http://www.cplusplus.com/reference/iostream/ifstream/is_open/) also produces the error-opening-file message:


// ifstream::is_open
#include <iostream>
#include <fstream>
using namespace std;

int main () {

ifstream infile;
infile.open ("testFile.txt");
if (infile.is_open())
{
while (infile.good())
cout << "file opened successfully" << endl;
infile.close();
}
else
{
cout << "Error opening file";
}
return 0;
}

And this program also fails to open the file:

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>

using namespace std;

void readInputFile (ifstream &infile, char buf[]);

int main(int argc, char *argv[])
{
string fileName;
fileName= "mystring";
cout << "What is the file's name?" << endl;
cin >> fileName;
ifstream infile;
infile.open(fileName.c_str());
char buf[1024]={0};

// Read file into array
readInputFile (infile, buf);

system("PAUSE");
return 0;
}

// FUNCTION DEFINITION

void readInputFile (ifstream &infile, char buf[]) {

if (infile.is_open())
{
while (! infile.eof() )
{
infile.read(&(buf[0]),1024);
}
cout << "File reading was successful" << endl;
infile.close();
}

else cout << "Unable to open file" << endl;
}

The strange thing is that similar code worked perfectly in the same computer and the same directory a few days ago. It seems that file opening somehow has stopped working.

I suspect the problem is not the code, but something related to the directory; but what it is exactly, I have no idea.

Any suggestions? Thanks in advance.
Couple questions:
1. Are you hiding extensions (i.e. is the file really called "textFile.txt.txt")? I know it sounds stupid, but this once kept one of my coworkers stuck for an hour before I took a look to see what was wrong.
2. Are you running from a console or debugging? If you're debugging, is the working directory set correctly?
Hi, Helios:

1. I've tried "testFile", "testFile.txt" and "testFile.txt.txt", but I still get the error-opening message. I'm sure the problem is going to turn out to be something simple like that, so thanks for the suggestion.

2. I'm running from a console.

These are some other things I've tried that haven't worked:

- I created a new folder with a new program, and included testFile.txt in that folder;
- I created a new Notepad (txt) document with a different name ("newTestFile" because I was feeling creative);
- Originally I was using DevC++, and I downloaded wxDevC++ (an upgraded and most recent version of that program). It didn't make any difference.

Thanks!
Update: I've tried checking the access permissions for the folder, and I have permission to access all the documents in the folder. The file still doesn't open.
I've come across a thread in another forum (http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/e0783923-988e-41ad-bfe7-a3ba157aab0a) written by someone with a similar problem. After getting different suggestions from others in the forum, the author writes:

"Thanks for the tips guys. I’m still not sure why it wasn’t working. I copied and moved the file right into the directory of the cpp file. I mean I dragged and dropped it right next to it and it still wouldn’t open it.

I worked around the problem by combining the program that creates the "client" file and the program that reads it and that worked perfectly. I checked the directory and the "client" file is in the same place that I had moved it to before. Strange!"

The problem is that I have no idea what the author means by "combining the program that creates the "client" file and the program that reads it". I have created a new message in that thread, but I haven't received any replies yet.

Any ideas?

Thanks.
Update: Writing the file's full path allows the program to open the file. Escape characters must be included after each '\' in the file's path; e.g., C:\\Users\\HG\\Documents\\myFile instead of C:\Users\HG\Documents\myFile.

Now the question is how to allow the use of only the file's short name (i.e. "myFile").
Topic archived. No new replies allowed.