I am now using Visual C++ 2010 to open a txt file.
fp = fopen("E:\\CProg\\Huffman\\Debug\\Huffman.txt","r"); //Right
//Wrong
//fp = fopen(".\\Huffman.txt","r");
//fp = fopen("\\Huffman.txt","r");
//fp = fopen("Huffman.txt","r");
In VB.NET, I used to write like this: Application.Startpath & "\". Then how do I make it in C?
Relative to what? On "all relevant platforms", if you use a filename that's not absolute it will be resolved relative to the current directory.
The following is for Windows.
If you are writing a console application, the application will start with the current directory set to whatever the command prompt shows. Without further research I can't tell what the initial current directory for a GUI application will be.
If you want your filename to be relative to the Installation directory you'll have to use something like the Win32 function GetModuleFileNameW() and work your way from there (the function gives you the pathname to the exe file; remove the last component to get the directory, and append whatever path you want to append)-
There is no direct way to say "I want this filename to be used relative to the installation directory".
Related
I have created a batch script that requires the location of 2 files to be passed into it. I would like it to work regardless of whether the user gives the full file path or relative file path of the files to be read in.
I've tried the following
set Var1=%~dp0%1
set Var2=%~dp0%2
which only works if relative file path is given. Without %~dp0obviously only works if full path is given.
Is there a way for either the full or relative path to be given and for my batch file to work.
Thanks
I'm working with C right now. And there's a problem. I don't know how to save a FILE in custom place. When I run *.exe file, it saves them where code is placed. So how to make it save FILEs where I want it to be?(I can input a path)
a FILE is actually a long type that addresses a path on your computer.
Whether you use linux, windows, etc, the common thing about the paths is the idea that there are relative paths or absolute paths.
From what I've understood you probably did use the relative path, and I can guess you didn't specified a path at all, but only the file name.
Notice that a file's name alone is placed relatively to the path of the program you are running.
To fix your problem you might want to give an absolute path (such as "/home/user/" on linux or "C:\Users\user" on windows [pay attention for the escaping backslash]).
You can do it by something like this:
FILE *output = fopen("/home/user/output.txt", "w");
(where "w" means writing permissions to the file at the given path).
Hope this answers your question.
I need to read file in my program so while providing path I want to give relative paths because all files to be opened will be in some folder within current folder.
I tried this:
FILE *f=fopen("./abc/p.txt","r")
abc is folder withing current folder, but fopen returns NULL. How to do this thing?
This comes from either one of those:
. or ./abc/ is not readable or traversable
./abc/p.txt is not readable
./abc/p.txt does not exist
./abc/p.txt is a broken link
Look at errno to know what's the real problem.
this will run:
FILE *f=fopen("...\\abc\\p.txt","r");
File open with fopen() fails when I provide a relative path. I am running this code on a windows machine with Visual Studio 2010.
const char* OUTPUT_FILE = "output/PERFORMANCE.txt";
FILE* f = fopen(OUTPUT_FILE, "w");
Is this way of specifying relative path incorrect? Should I be using "\" separator?
Specifying the absolute path using the same format works fine. (e.g "C:/output/PERFORMANCE.txt")
Check what directory you are currently in. With Windows there are at least two ways:
_getcwd
GetCurrentDirectory
Make sure you are in the directory you think you are, and make sure that the directory output exists in the directory, otherwise you'll need to create it. With Windows, there are at least two ways:
_mkdir
CreateDirectory
The file will fail to open (even for writing) if any intermediate directories are not present. The file may also fail to open if you do not have permissions to the target directory.
Does the output subdirectory exist? Opening a file for write will create the file if it doesn't exist, but not the path to it.
I have a C console app where I need to output the absolute path to a file given a (possibly) relative path. What is the best way to do this in C in a Windows environment?
I think you are looking for _fullpath().
GetFullPathName should help you on Windows.
GetFullPathName merges the name of the current drive and directory
with a specified file name to determine the full path and file name of
a specified file.