I am have a trouble in using fopen() with relative path. I wanted to use fopen like this:
fopen("\\Saurabh\\pqrs.txt");
i am getting filePointer as null.
The situation arose because I am trying to create a setup or deployment project which has to read files. The file paths chosen by default after user executes setup are C:\Program Files\Setup..
(where exe is dumped). So I dumped the files in the same folder and gave path(fixed path or hardcoded) to those files in the program.
If the user selects some other path for installation, the program fails.
Is there any way I can fix this?
Two problems:
You need to escape the backslash character. Write \\.
You need to use a relative path. By starting a path with \\ you mean start from the root directory.
Putting these together, I think you should write:
fopen("Saurabh\\pqrs.txt");
Make sure you double up the \ characters. The string you're passing in should be "Saurabh\\pqrs.txt". Note that beginning a path with \\ means that it's not relative. (Well, actually it's relative to your current drive, but I doubt that's what you're looking for.)
Related
I have a program that I'm attempting to write various files to. In an attempt to clean it up I would like to put these files into a directory. My program is running in a directory different from the sln for VS 2012 i.e. the exe is the only part of the VS files in the directory. If I simply print the files, it's fine but if I attempt to print those same files to a different directory, then it doesn't do anything. I printed out the directory that I'm attempting to access and I'm getting "000000" as a response. code looks like:
std::ofstream output
output.open("\myFiles\entityOutput.csv", std::ios::app);
output << "print some stuff here" << std::endl;
I'm sure it's something simple but I haven't been able to figure out what. Tried "\\myFiles....." already "/myFiles" etc. I'm on a Windows 7 system as well.
That's because, while opening a file, you need to swap \ with either \\ or /. The reason is that \ character is considered as a special one that's used for example to represent end of line character (\n).
Just swap output.open("\myFiles\entityOutput.csv", std::ios::app);
to output.open("/myFiles/entityOutput.csv", std::ios::app);,
however make sure that the path is also correct!
Usually it's a good programming practice to check whether or not a file opening succeeded at first.
I'm trying to load files, and previously I was using hardcoded file locations, (like "c:\location\file.txt") but now that a few friends are also using the file, I'd like to allow them to put the executable wherever they want.
my current code looks like:
ifstream myfile;
myfile.open("c:\\client\\settings.cfg");
I'm trying to change it so that the user puts their executable into whatever folder they want, and then they create a folder and put their settings file into it and the exe will load that with their settings.
ifstream myfile;
myfile.open("\\settings\\settings.cfg");
I have some basic error handling in place, and now the program always errors out saying that it can't find the file.
The file structure looks like this:
[ART]
asset.png
[SETTINGS]
settings.cfg
client.exe
This seems like a really simple thing to do, but I can't find any way to do it. Every example and tutorial about reading and writing to files deals only with files in the executable's directory, or hardcoded into c:\folder...
Could anyone point me to how I do this?
The search path for most systems starts with the current working directory and then to a PATH environment variable. So, all you need to do is specify the file/folder without the absolute path markings and it will use the path relative to the working directory:
ifstream myfile;
myfile.open("settings\\settings.cfg");
// ^^ Note the lack of \\ to start the file path
Paths beginning with \ are always relative to the current drive's root directory. If the current drive is C:, then \settings\settings.cfg means C:\settings\settings.cfg.
Note that you can use / in order to avoid escaping everything. So you can use: settings/settings.cfg. This will be relative to the user's current directory. Note however, that this doesn't necessarily correspond to the directory where the executable resides. If you need the directory of the executable, then you need to use a Windows API function to get it:
#include <Windows.h>
// ...
HMODULE module = GetModuleHandleW(NULL);
WCHAR path[MAX_PATH];
GetModuleFileNameW(module, path, MAX_PATH);
Now if you want to open settings/settings.cfg relative to the directory of the executable, create a path that starts with path and append /settings/settings.cfg to it.
I have problem when I try to compile my project in Code Blocks with wxWidgets, I was looking everywhere solution for my problem but nothing was good for me and nowhere was explained how to solve this problem...
I post image of my problem here http://postimage.org/image/ngejv16d3/
Agree with greatwolf. Or you can use DOS path instead (in command prompt go to C:\ and execute dir /x, it will display the DOS version of Program Files (x86) folder (without spaces). Then you can change your wx path accordingly.
The fully qualified path of your wxWidget location is getting truncated, most likely due to spaces in the path. Two possible ways to fix it, you can move your wxWidget install to another directory with the spaces removed or you can try enclosing your current path in "" double quotes when it gets passed to the compiler.
My goal is to build a program that renames all files in the current working directory so they don't have any spaces, any special characters or any accented characters (for example É would become E). I'm planning on using int rename(const char *oldname, const char *newname); . My problem is how do I get the files in the current working directory? I would like to have the executable I'm creating put in a folder with a files with bad names and run it and the files all be renamed.
A platform independent solution would be preferable, otherwise I'm using Windows 7 Enterprise 32bit.
This question isn't a duplicate because I don't know the path for opendir ("c:\\src\\"); it's whatever directory the program is being executed from.
Here's a sample code to do that:
http://bytes.com/topic/c/answers/869208-list-files-directory
In essence you utilize these APIs: FindFirstFile and FindNextFile
For cross-platform solution see findfirst() and findnext()
An option is to use opendir(".") , this will open the current directory.
I have my visualstudio vcproj file at c:\vsproj\example\test\test.vcproj
under this path i have some other files like e test.cpp file and also a dll test.dll is there.
so totally under tha path c:\vsproj\example\test i have
1) test.vsproj 2) test.dll 3) test.cpp
normally to get the cuurent folder path we use ".\" so i have applied the technique to get
the dll path which is reside where the test.cpp file is there
now in test.cpp some where else i have written
string str= ".\\test.dll" to get the test.dll path. But i am not getting the dll path into the
my idea is i have to get the path in the variable str="c:\vsproj\example\test\test.dll"
but i am getting ".\test.dll" wat is wrong can u correct me??
variable str; how to get the dll path that in this scenario...
string str= ".\test.dll" to get the
test.dll path. But i am not getting
the dll path into the
my idea is i have to get the path in
the variable
str="c:\vsproj\example\test\test.dll"
but i am getting ".\test.dll" wat is
wrong can u correct me?? variable str;
how to get the dll path that in this
scenario...
It's doing exactly what you're asking it to do. What you appear to want it to do isn't going to be achieved this way.
You'll need to get the current working directory and prepend it to "\test.dll", rather than just specifying "\test.dll".
check with Filemon program. it shows you where the code is trying to search and show you where you do wrong..
String has nothing to do with paths. How could it understand what you want? It is just a mere collection of letters.
As others suggested, try using _getcwd and appending "\test.dll" at the end of it.
The function you're looking for is GetFullPathName(). It works on C strings, not C++ strings though. Have a look at the examples in the linked article.
(You can safely ignore the panicky bits about multi-threaded applications. The same problem actually exists for single-threaded app too. If the current directory changes, ..\xyz\ changes too.)
you can use _getcwd(char* buf, int len) to get the current working directory: