0

So i'm trying to read what's inside a file in my contents folder of my application:

std::string username;
std::ifstream user("../Resources/username.txt");
user >> username;
user.close(); /*here I do a breakpoint to check the value of username and it's ""*/

I do a "if" to check if the file is open, it's obviously not can. you tell me what i'm doing wrong (i know the file path and its name are correct I used similar processes for image upload ect with SDL2 ect)

user2826636
  • 41
  • 1
  • 7
  • you are missing "std::ifstream::in" in you std::ifstream user("../Resources/username.txt", std::ifstream::in); Other than that your example works for me in VC++ – Rob Nov 12 '13 at 04:54
  • I'll test that as soon as xcode finish update, but what should i put for ofstream? std::ofstream::out/in? – user2826636 Nov 12 '13 at 04:58
  • `ifstream` and `ofstream` default to in and out respectively, so that's not the issue. Are you sure the file is actually being opened? You never check. – Retired Ninja Nov 12 '13 at 05:09
  • Well I tested it and exact same problem. As I said before I tested if the file opens and it does not – user2826636 Nov 12 '13 at 05:12
  • Tested what? You checked if the file was open and it was or wasn't? When you're trying to track down a problem it is best to perform as much error checking as possible. In this case checking to see if the file is good before reading is one place, and the actual read is another place where you could check for errors and you do not. What happens if you put the file in the same directory as the executable and try and open it with just the filename and no relative directory? – Retired Ninja Nov 12 '13 at 05:16
  • I did a if test on the stream to see if it was open, and apparently it was not since it did not return true – user2826636 Nov 12 '13 at 05:19
  • I tried this: std::string username; std::ifstream user("../Resources/username.txt"); if(user) { user >> username; /*breakpoint here*/ } user.close(); – user2826636 Nov 12 '13 at 05:24
  • 1
    Well, `fstream` isn't broken and your code seems ok, so the only thing left is that the file is not where you think it is. Take another careful look at your directory structure and see where you've gone wrong. – Retired Ninja Nov 12 '13 at 05:48
  • Did you set the project scheme to use the folder where your file resides as the working directory at startup? It is *not* the project folder by default, and in fact is some distant temp folder where the executable was linked. For information on setting the project scheme to use a custom working directory at startup, [see this question and answer](http://stackoverflow.com/questions/14476655/code-runs-perfect-in-g-but-not-in-xcode-cannot-find-file/14478210#14478210). – WhozCraig Nov 12 '13 at 07:08
  • I did, and the path work for loading other things (pictures, sounds, musics) – user2826636 Nov 12 '13 at 13:31

1 Answers1

0

try this:

std::string username;
std::ifstream user("C://test.txt", std::ifstream::in);

user >> username;

user.close(); /*here I do a breakpoint to check the value of username and it's ""*/
Dean
  • 499
  • 6
  • 13
  • 34