Uploading a file issue

Hey all for some reason the file is always bad. its a plain text file. Any help would be great

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#include <cctype>
#include <cstdlib>

using namespace std;

const string FILE1 = "/Users/batstoned/Desktop/p1.txt";

int main()
{

  ifstream infile;
  infile.open(FILE1.c_str());

  if(infile.fail()) {
    cout << "error";
  } else {
    cout << "cool" << endl;
  }

  return 0;
}
Are you sure that the file path is valid? That seems to look like a bit of a dodgy file path, starting with a slash and all (you obviously aren't running a linux OS)
Last edited on
Im on a mac...

the path copied from the info window: /Users/batstoned/Desktop
and the file is named p1.txt
I've since added a infile.close(); to no avail
Don't use ios::fail() to determine if your file opened successfully. Use ifstream::is_open() instead.
tried it Lodger , again no headway
Ok, obvious question - is "/Users/batstoned" the currently active user account? i.e. the user account you're currently logged into. (Stupid question perhaps but it's worth asking anyway)

Do you have the necessary permissions to open the file? i.e. When you "Get Info" on the file, does everyone have "Read & Write" access under "Sharing and Permissions"
You shouldn't use is_open() either.
This is it:
if(infile) {

Besides, use a full path instead.
/Users/... is invalid unless your current folder is C:.
Last edited on
ahh not working edited it to this:

#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#include <cctype>
#include <cstdlib>

using namespace std;

const string FILE1 = "C: /Users/batstoned/test.txt";

int main()
{

ifstream infile;
infile.open(FILE1.c_str());

if(infile) {
cout << "sex";
} else {
cout << "fuck off" << endl;
}

infile.close();
return 0;
}




You have a space in the path after C: . Remove it.
@EssGeEich
That shouldn't matter, look at what @Jbat04 said earlier:
Im on a mac...
If you're on a Mac, /Users/... is a perfectly valid path. C: doesn't exist
Sorry, I have no idea how Mac's paths work, it just looked like W7's folders structure.
So to wrap up this thread. What I ended up doing was just making a txt file on the same console thing instead of inputting form the computer memory. I think there was something blocking the access to the computer memory. Thanks for the help all
So you didn't figured out what the problem is.
Topic archived. No new replies allowed.