Jump to content
  • Advertisement
Sign in to follow this  
chbrules

[C++]stdio fopen() and std::string

This topic is 4816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It seems I'm having issues when I try to open a file for reading using fopen() when I pass the filename to it as a std::string. I do use the member function c_str() to convert it to the proper c-string format, but during runtime I don't get the proper results. I've debugged it and the filename is proper, I just don't know why it's not opening it. Does fopen() need the .\ to signify the same folder for opening a file? Right now, for example, I'm trying to open a file in the same directory as the flag file like so: "CampaignID000005-OpenedEmail.txt" I've debugged it and seen the values are proper of the filename, so I don't believe it's a naming issue. Either way, when it goes to open the file it's apparently returning a NULL to the FILE pointer I made. I've even tried manually just putting the filename in there using a literal string, nothing. Any ideas? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Are you sure that the file is in the current working directory? Some IDEs debug the executable using a different directory as the working directory than the location of the executable.

Share this post


Link to post
Share on other sites
Try full-qualifying the path. If that gets you access to the file, then your relative-pathing is broken someplace.

Also, I'd personally recommend against mixing the C file functions with C++/STL unless you absolutely must. It's partially a matter of style, but using the STL iostream classes gives you access to a lot more certainty via things like RAII and guaranteed type-safe reads/writes.

Share this post


Link to post
Share on other sites
I've had problems because of not replacing \ in full paths with \\ ( ".\file.txt" should be ".\\file.txt" because \ is an escape charactor)

Share this post


Link to post
Share on other sites
Note that you can use forward slashes ( for example "./data/sky.png" ) even in Windows, which saves you from having to remember to escape all of them.

Share this post


Link to post
Share on other sites
Quote:
Original post by me22
Note that you can and should use forward slashes ( for example "./data/sky.png" ) even in Windows, which saves you from having to remember to escape all of them.


Fixed. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!