Uploading a file issue

Jan 22, 2014 at 9:41am
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;
}
Jan 22, 2014 at 9:46am
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 Jan 22, 2014 at 9:46am
Jan 22, 2014 at 9:52am
Im on a mac...

the path copied from the info window: /Users/batstoned/Desktop
and the file is named p1.txt
Jan 22, 2014 at 10:18am
I've since added a infile.close(); to no avail
Jan 22, 2014 at 10:19am
Don't use ios::fail() to determine if your file opened successfully. Use ifstream::is_open() instead.
Jan 22, 2014 at 10:28am
tried it Lodger , again no headway
Jan 22, 2014 at 12:32pm
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"
Jan 22, 2014 at 12:42pm
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 Jan 22, 2014 at 12:44pm
Jan 22, 2014 at 11:41pm
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;
}




Jan 23, 2014 at 11:01am
You have a space in the path after C: . Remove it.
Jan 23, 2014 at 11:45am
@EssGeEich
That shouldn't matter, look at what @Jbat04 said earlier:
Im on a mac...
Jan 23, 2014 at 11:45am
If you're on a Mac, /Users/... is a perfectly valid path. C: doesn't exist
Jan 23, 2014 at 9:04pm
Sorry, I have no idea how Mac's paths work, it just looked like W7's folders structure.
Jan 24, 2014 at 8:00am
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
Jan 24, 2014 at 8:54am
So you didn't figured out what the problem is.
Topic archived. No new replies allowed.