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

Started by
5 comments, last by Zahlman 18 years, 7 months ago
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!
-Conrad
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.
Yes I've executed it inside the folder manually as well.

edit: fyi I'm using MSVC++ .NET 2003
-Conrad
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I've had problems because of not replacing \ in full paths with \\ ( ".\file.txt" should be ".\\file.txt" because \ is an escape charactor)
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.
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. :)

This topic is closed to new replies.

Advertisement